📜  红宝石 |数组类each()操作

📅  最后修改于: 2022-05-13 01:55:30.396000             🧑  作者: Mango

红宝石 |数组类each()操作

Array#each() :each()是一个 Array 类方法,它通过遵循给定块中的条件一次为 self 中的每个元素返回数组。

Syntax:  Array.each()

Parameter:  block - condition to follow

Return:  Each array element following the condition

代码 #1:each() 方法的示例

# 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"

输出 :

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

代码 #2:each() 方法的示例

# 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"
     

输出 :

each : ["hello", "hi", "dog"]