Ruby | Array class each() operation

Array#each() : each() is a Array class method which returns the array by following the condition in the given block once for each element in self.

Syntax:  Array.each()

Parameter:  block - condition to follow

Return:  Each array element following the condition

Code #1 : Example for each() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for each() method
  
# declaring array
a = ["abc", "nil", "dog"]
  
# declaring array
b = ["hello", "hi", "dog"]
  
# each
puts "each : #{a.each {|x| print x, " -- "}}\n\n"

chevron_right


Output :

abc -- nil -- dog -- each : ["abc", "nil", "dog"]

Code #2 : Example for each() method

filter_none

edit
close

play_arrow

link
brightness_4
code

# Ruby code for each() method
  
# declaring array
a = ["abc", "nil", "dog"]
  
# declaring array
b = ["hello", "hi", "dog"]
  
  
# each
puts "each : #{b.each{|x| x = 2}}\n\n"
     

chevron_right


Output :

each : ["hello", "hi", "dog"]
My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.