Ruby 数组类each()操作
Array#each() : each()是一个数组类的方法,通过对自我中的每个元素按照给定块中的条件进行一次返回数组。
语法。 Array.each()
参数:block – 要遵循的条件
返回。 跟随条件的每个数组元素
代码 #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"
```
输出:
```ruby
each : ["hello", "hi", "dog"]