we use something called a
while loop. It checks to see if a certain condition is true, and while it is, the loop keeps running. As soon as the condition stops being true, the loop stops!2.Danger: Infinite Loops!
infinite loop and it will cause your programs to crash. Avoid them at all costs!
i = 0
while i < 5
puts i
i = i+1
end
3.The 'Until' Loop
The complement to the
while loop is the until loop. It's sort of like a backwards while:i = 0
until i == 6
i += 1
end
puts i
- In the example above, we first create a variable
iand set it to zero. - Then we execute a block of code until
iis equal to6. That block of code incrementsi. - When
iis equal to 6, the block ends. - Finally, we print
6, the value ofi, to the console.
4. More Assignment Operators
A shortcut is to use an assignment operator. You can update a variable with additional assignment operators, however, which include
+=, -=, *=, and /=.counter = 1
while counter < 11
puts counter
counter += 1
end
'For' Loops
5.The 'For' Loop
for num in 1...10
puts num
end
6. Inclusive and Exclusive Ranges
The reason Ruby counted to 9 and not 10 was because we used three dots in the range; this tells Ruby to exclude the final number in the count:
for num in 1...10 means "go up to but don't include 10." If we use two dots, this tells Ruby to include the highest number in the range.Iterators & Blocks
8.The Loop Method
It's also possible to repeat an action using an iterator.
The simplest iterator is the
loop method. You can create a basic (but infinite!) loop by simply typingloop { print "Hello, world!" }
In Ruby, curly braces ({}) are generally interchangeable with the keywords do (to open the block) and end (to close it). Knowing this, we can write a smarter loop than the one above:i = 0
loop do
i += 1
print "#{i}"
break if i > 5
end
The break keyword is our Get Out of Jail Free card: it breaks a loop as soon as its condition is met.9.Next!
The
next
keyword can be used to skip over certain steps in the loop. For
instance, if we don't want to print out the even numbers, we can write:for i in 1..5
next if i % 2 == 0
print i
end
10.Saving Multiple Values In Ruby, we can pack multiple values into a single variable using an array. An array is just a list of items between square brackets, like so:
[1, 2, 3, 4]. The items don't have to be in order—you can just as easily have [10, 31, 19, 400].my_array = [1,2,3,4,5]
11.The .each Iterator
A more useful iterator is the
.each method, which can apply an expression to each element of an object, one at a time. The syntax looks like this:
object.each { |item| # Do something }
You can also use the do keyword instead of {}:object.each do |item| # Do something end
The variable name between | | can be anything you like: it's just a placeholder for each element of the object you're using .each on.array = [1,2,3,4,5]
array.each do |x|
x += 10
print "#{x}\n"
end
12.The .times Iterator
The
.times method is like a super compact for loop: it can perform a task on each item in an object a specified number of times.For example, if we wanted to print out
"Chunky bacon!" ten times, we might type10.times { print "Chunky bacon!" }
13.Project
13.1.Getting the User's Input
puts "text"
text = gets.chomp
puts "redact"
redact = gets.chomp
13.2.The .split Method
Ruby has a built-in method for this called
.split; it takes in a string and returns an array. If we pass it a bit of text in parentheses, .split will divide the string wherever it sees that bit of text, called a delimiter. For example,text.split(",")
tells Ruby to split up the string text whenever it sees a comma.13.3.Redacted!
Let's start simple: write an
.each loop that goes through words and just prints out each word it finds.words.each do |word|
print word
end
13.4.Control Flow Know-How
ifthe current word equals the word to be redacted, thenprint "REDACTED "with that extra space.- Otherwise (
else),print word + " ".
print "REDACTED "
else
print word+" "
end
No comments :
Post a Comment