When teaching Ruby I always like to work on some programming challenges together to test the problem-solving skills of my students.
The main problem I see is that they don’t know how to think like a programmer & they aren’t familiar with the tools available to us.
Which tools I’m referring to?
I’m not talking about your editor, plugins, git & other supporting tools, I mean the tools your programming language offers you to be able to solve problems:
Data structures & functions (methods in Ruby).
A “data structure” may sound like some fancy Computer Science term but it’s just a certain way to organize data in your computer memory.
A method allows you to take data inside a data structure & transform it in some way.
We’re going to make this concrete with a simple example.
Counting Numbers
Let’s say you’re given the task of counting numbers in a given list.
The list looks something like this:
num = [2,2,3,2,4,5,4]
How would you approach such a task as a Ruby beginner? Well you could start by thinking about how to solve this on a piece of paper, for example:
2: | | | 3: | 4: | | 5: |
One way you could do this is that every time you find a number you write it down if you didn’t see it before & add a vertical bar beside it to represent “count = 1”.
If you saw the number before you simply add another bar to the corresponding number.
Now…
How could we translate this into Ruby?
Well, if you have studied your data structures you may recall Hashes.
A hash allows you to store data in your computer memory in a very particular way: Like a dictionary, with keys (words) & their corresponding values (definitions).
If you look at what we did in our pen & paper exercise… doesn’t it look a lot like keys (the numbers) & values (the vertical bars)?
Yes! That’s exactly right.
The Syntax Problem
Ok, so far we figured out that a hash data structure would be adequate for storing our number counts, but the next issue that beginner developers run into is syntax.
What is syntax?
Syntax is the set of rules on how to properly communicate in your programming language of choice.
This is something that you simply have to know or something you have to look up, for example, you could Google: “Ruby hash syntax“, there’s NOTHING wrong with looking it up & it beats using something lame like AI which could give you the wrong answer.
Here’s the syntax for creating an empty hash:
count = {}
Yep, it’s as simple as that. Notice these are CURLY brackets & not regular parenthesis.
This is important, if you don’t have the proper syntax you’ll get syntax errors. That doesn’t mean that you’re stupid or anything like that! It means that you’re learning & need to study more 🙂
Btw even experienced developers get syntax errors, so don’t worry about it, just find out how to fix them & keep going.
Next!
We have an empty hash now but we need to add things to it, you can do this again with the proper syntax (if you don’t know, you could Google “ruby hash how to add values”).
count[2] = 1
This line of code would be the equivalent of “writing down a new number when we find it & giving it the value of one”.
Now:
We need the appropriate code for adding to the count of an existing number (one we have already seen).
count[2] += 1
Yes, the only difference is the “+” symbol, which means “add one to the current value”.
We need to do something different for these two cases (the first time we find a value & after we have already seen it) because we can only use “+=” if there is a current value, that’s why we have to initialize to 1.
The last step will be to use a loop so that we can go over every element of our input array (numbers
) & add a simple conditional check (to find out if something is true or false) inside the loop.
Looping & Checking for Values
In Ruby we normally don’t write loops using while
or for
like in some other programming language because we have a more powerful way to do it: Enumerable methods.
These are methods like each
, select
& map
.
For example, this code will take an array (a data structure that stores a list of objects) & allow you to process it one by one which is exactly what we want.
[1,2,3,4,5].each do |i| puts i end
This code contains very important syntax elements, first, we have the array, followed by the method we want to use on it (each
), notice the dot (.
) between them.
Then we have a do
, which signifies the start of the loop & the end
later on signifies the end.
The |i|
after do
is the “block parameter”, this is how we give a name (in this case i
) to the current value we’re working with, in other words, it will become every element of the array.
Finally, the puts i
prints the value of the current element i
.
Notice that all the code within the do
/ end
block will be repeated for every element of the input, so if the input array has 5 elements this loop will run 5 times so the puts i
instruction will run 5 times.
To finish our Ruby program today all we need is a simple if
/ else
/ end
conditional statement to check whether our count
hash already contains a value so we can act appropriately.
Final Code
Here’s the final solution by putting all the pieces together:
numbers = [2,2,3,2,4,5,4] count = {} numbers.each do |i| if count.key?(i) count[i] += 1 else count[i] = 1 end end p count
I hope you learned something useful, now don’t just read, be an active student, take notes, research any unfamiliar terms & topics, and create your own code examples!
Thanks for reading & have a nice day 🙂