Science, Tech, Math › Computer Science Using the Each Method in Ruby Share Flipboard Email Print vgajic/Getty Images Computer Science Perl PHP Programming Python Java Programming Javascript Programming Delphi Programming C & C++ Programming Ruby Programming Visual Basic View More By Kirk Brown Kirk Brown Computer Science Expert B.A., Abilene Christian University Kirk Brown is a systems software engineer with expertise in Perl, PHP, Ruby, and Python. Learn about our Editorial Process Updated on January 20, 2019 Every array and hash in Ruby is an object, and every object of these types has a set of built-in methods. Programmers new to Ruby can learn about how to use the each method with an array and a hash by following the simple examples presented here. Using the Each Method With an Array Object in Ruby First, create an array object by assigning the array to "stooges." >> stooges = ['Larry', 'Curly', 'Moe'] Next, call the each method and create a small block of code to process the results. >> stooges.each { |stooge| print stooge + "\n" } This code produces the following output: Larry Curly Moe The each method takes two arguments—an element and a block. The element, contained within the pipes, is similar to a placeholder. Whatever you put inside the pipes is used in the block to represent each element of the array in turn. The block is the line of code that is executed on each of the array items and is handed the element to process. You can easily extend the code block to multiple lines by using do to define a larger block: >> stuff.each do |thing| print thing print "\n" end This is the same as the first example, except that the block is defined as everything after the element (in pipes) and before the end statement. Using the Each Method With a Hash Object Just like the array object, the hash object has an each method that can be used to apply a block of code on each item in the hash. First, create a simple hash object that contains some contact information: >> contact_info = { 'name' => 'Bob', 'phone' => '111-111-1111' } Then, 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 produces the following output: name = Bob phone = 111-111-1111 This 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, these elements are placeholders that are used to pass each key/value pair into the code block as Ruby loops through the hash. You can easily extend the code block to multiple lines by using do to define a larger block: >> contact_info.each do |key, value| print print key + ' = ' + value print "\n"end This is the same as the first hash example, except that the block is defined as everything after the elements (in pipes) and before the end statement. Cite this Article Format mla apa chicago Your Citation Brown, Kirk. "Using the Each Method in Ruby." ThoughtCo, Aug. 27, 2020, thoughtco.com/using-each-beginning-ruby-control-structures-2641202. Brown, Kirk. (2020, August 27). Using the Each Method in Ruby. Retrieved from https://www.thoughtco.com/using-each-beginning-ruby-control-structures-2641202 Brown, Kirk. "Using the Each Method in Ruby." ThoughtCo. https://www.thoughtco.com/using-each-beginning-ruby-control-structures-2641202 (accessed June 5, 2023). copy citation