Code Blocks
Code blocks provide structure for executable statements in Ruby. Blocks are defined by the starting and ending keywords, "do" and "end", respectively. The code between the keywords is what gets executed. Code blocks allow multiple lines of code to be written. There is no limitation to how many lines may be executed as long as each line of code is syntactically correct. Here is an example of a code block:
>> 5.times do
>> puts "Hello"
>> end
# What will this code output??
When I'm learning a new language I keep a notebook where I can physically write things down. It's old school but it is really helpful. By writing it down you can't fall victim to the copy/paste syndrome and you actually spend more time with the information. This helps you remember it. It also provides a useful reference for measuring your growth in the language.
Let's examine this code
On the first line, we see " 5.times". The word "times" is to the right of the decimal so it must be a method that is being called by the integer five. If we come across a method we aren't familiar with we can check the Ruby documentation. By navigating to Ruby Docs we can see that Integer has a number of public methods, "times" being one of them. The definition of "times",
"Iterates the given block
int
times, passing in values from zero toint - 1
."
We see the example provided and now have a better understanding of the public method.
Reading documentation is an important part of software and web development. The documentation provided may not always be the highest quality information but it does come straight from the source. It's a good place to reference information that you are unsure of or to learn about a topic more in-depth. You might also find that while you are referencing some documentation that you learn something completely new. For example, the method right under "times" is "to_f". This method converts an integer to a floating point number. A few more methods down is the "to_s" method. This method converts the integer to a string. These are handy methods to know about. Jot them down. The next time you get an error that reads, "No implicit conversion of type Integer to type String", you'll likely remember there is a method that allows you to do just that!
A code block can be written a second way replacing the do and end keywords with curly braces { }
. We saw an example of this in the chapter Conditional Execution, which read:
fruits.each { |fruit| puts fruit }
The use of curly braces allows the code to be written on a single line. The ruby interpreter knows what code to execute because it looks for the code between do and end keywords, or code inside of curly braces. Here is the alternative way of writing the example using a code block:
fruits.each do |fruit|
puts fruit
end
# produces the same result as example above
Block Variable Scope
This is good place to stop and reconsider variable scope. We touched on the concept of scope in the chapter on Variables. The scope of a variable is where in the program it can be accessed. In the last two examples we used a block variable, "fruit". The scope of this variable is the code block in which it is defined. We wouldn't have access to this variable after the code is finished running. It's a temporary variable that Ruby uses once and then discards.
In the examples, we created a temporary variable to be yielded to the code block, which prints out the variable. The variable 'fruit' could have been 'x', 'a', or whatever the developer chooses as long as the variable between the pipes is the same as the variable in the code block.