>> contact_info = { 'name' => 'Bob', 'phone' => '111-111-1111' }Then we'll call the each method and create a single line block of code to process and print the results.
>> contact_info.each { |key, value| print key + ' = ' + value + "\n" }This will produce the following output:
name = BobThis works exactly like the each method for an array object with one crucial difference. For a hash, you create two elements - one for the hash key, and one for the value. Like the array each method, these elements are placeholders that are used to pass each key / value pair into the code block as Ruby loops through the hash.
phone = 111-111-1111
You can easily extend the code block to multiple lines by using do to define a larger block:
>> contact_info.each do |key, value|This is exactly the same as the first example, except that the block is defined as everything after the elements (in pipes) and before the end statement.
.. print print key + ' = ' + value
.. print "\n"
.. end
Previous: Looping through an array in Ruby with the each method
