Week 2 was certainly more difficult, there was a lot more material. Not unmanageable, but at times it felt a little overwhelming. I feel I came out with a good understanding of a number of different things – I’ll go into further detail below.
Let’s look at some examples from the past week, there certainly are more than can be summarized in this one blog post, so look forward to more updates this week.
– Loops –
We worked with
while loops and
.times loops. It may seem a little daunting at first but let’s break down a quick example of a
In this we are trying to do something pretty basic. We prompt the user to give their name and favorite number, then we set up a counter that will 0utput numbers from 1 to the number chosen by the user. Simple, right?
puts. In Ruby, puts will print a line of text to the screen and then jump to the next line, as opposed to
puts is shorthand Ruby for “Put String”.
We set a variable to house the user’s input. In Ruby this is done with
gets.chomp, this means that
fav_num_str is going to be
equal to whatever the user inputs in the
We make a NEW variable and name it
fav_num, and make THAT equal to whatever
fav_num_str is…EXCEPT now we are modifying the
class of that variable such that whatever the user inputs we will command the computer to convert it to an INTEGER using the
.to_i method. This has a host of similar “siblings” like
.to_s (String) or
.to_f (Float, which is any decimal number like
This means, for instance, if the user inputs
5.0, the computer will convert it to
5. If they type
house it will convert it to ZERO, but that is for another post.
Moving on we set a
0, and say that
while the counter (Which at this point still equals
< the value inputted by the user we will execute the following code.
We then add
1 to the counter total,
0 - now bringing the counter value to
1. Then we
puts the counter (In this case 1 – so it will print to the screen: 1), then the loop ends, BUT ONLY IF when it gets back to the top of the loop, counter is GREATER THAN our new variable
fav_num which is equal to the USER INPUT VALUE.
Check out these resources:
- What does gets.chomp do?
- gets.chomp basics
- Incrementing in Ruby with a Counter
- Ruby: Loops (while, for, .each, times, until)
- Learn Ruby The Hard Way: While Loops
– Operators –
There are also, different types of operators when working inside your loops. Here is a list of different operators you will likely encounter depending on the problem you are working on:
– .each method –
Now, what if we have an array (ex.
time_table = [1,2,3,4,5,6,7,8,9,10]) and we want to print each value in the array in order? One way is to use an
.each method. It is a more streamlined and hassle free version of the while loop. It would work something like this…
And it really is that simple. You name your Array (in this case) and attach to that
.each , in doing so, you will now run through EACH item in your array. Get it? Each. From there you open and close your LOOP with the
do/end syntax. Now, you may be wondering, what the hell is that
|num|? Well, the syntax for this method states that we identify what we are running through. So, in the case of
.each methods, it wouldn’t matter if
|basketball|, just so long as when we referenced it later in our code block, it matched with whatever we initially called it as. It is the identifier of each iteration of the array. so when it says
puts num, it will cycle through each item in the array and
puts num or
1 then 2 then 3 and so on.
Check out these resources:
It can seem difficult at first but it gets easier especially as you begin creating random loops, phrases, etc in your free time. Try to do things yourself, do all of the examples provided to you from whatever source you are learning from but then apply that knowledge to just create random things that you aren’t really going to use for anything.
The point is to burn the capabilities of these functions and methods into your head! So get going, time’s a wastin’!