Skip to content

Instantly share code, notes, and snippets.

@JoseJRVazquez
Created April 13, 2014 20:22
Show Gist options
  • Save JoseJRVazquez/10600736 to your computer and use it in GitHub Desktop.
Save JoseJRVazquez/10600736 to your computer and use it in GitHub Desktop.
LOOPS: Doing shit over and over again since computers first started
SO the lesson defines it as the following:
A loop is a programming construct that is used to "step through" a collection of objects. The proper way to express this in programming terms is to say that a loop "iterates over a collection." Another way to think about loops is that they instruct a computer to do a task multiple times. As you know, an Array is a collection of objects, so you could use a loop to iterate over an Array. While iterating over a collection of objects, a loop can be programmed to do things to each object in the collection.
Each
There are different ways to program loop constructs in Ruby, but the two most common are each and while. The each method is called on a collection, like an Array. Try the following in irb:
So gave their shit a try and here is is
2.0.0-p451 :244 > a = [1, 2, 3, 4]
=> [1, 2, 3, 4]
2.0.0-p451 :245 > a.each do |num|
2.0.0-p451 :246 > p "The num argument is assigned to: #{num}"
2.0.0-p451 :247?> end
"The num argument is assigned to: 1"
"The num argument is assigned to: 2"
"The num argument is assigned to: 3"
"The num argument is assigned to: 4"
=> [1, 2, 3, 4]
2.0.0-p451 :248 >
SO, the Each command made eah one of those print each one of the numbers in the array and
Tried this in context by doing this:
2.0.0-p451 :253 > a #this is the varible = ["basic", "free", "founding", "elite", "basic", "free"] #this is the array the variable "a" is assigned to
2.0.0-p451 :254 > a.each do |num| # called each method on the "a" array, and passit a block of code. The block started with "do", and the argument was "num" to be used in the block
2.0.0-p451 :255 > p "Your account level is #{num}" #inside the loop, a string interpolation with the "num" argument.
2.0.0-p451 :256?> end # with each iteration of the items within the array, it uses each one of the array items in turn with the string interpolation | line six closes it off when done
"Your account level is basic"
"Your account level is free"
"Your account level is founding"
"Your account level is elite"
"Your account level is basic"
"Your account level is free"
=> ["basic", "free", "founding", "elite", "basic", "free"]
2.0.0-p451 :257 >
And it worked: I guess it has its usefulness
SO onto WHILE
The while loop is weird
names = ["Steve", "Bill", "Sergey"] #created variable named "names" and assigned it to the array of strings
index = 0 #variable named index created, and it was assigned the value of "0" This is used to keep track of positions in the array
while names[index] #Here we declare a WHILE loop. The condition to be evaluated names array and the index evaluates the position within the array to decide if something is there in that position
p names[index] #prints ouf the element at the position given in the NAMES array
index += 1 #increments the index variable by 1; so with each time the items loops, the variable changes by 1
end
This one takes the array, adds one number to each iteration, and ends when the items run out, or the WHILE loop will not execute after the value of INDEX becomes greater than the number of items in the array. In this array, htere is nothin in the 3 position, so it stops at 2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment