为数组中的每个元素调用一次块,将该元素作为参数传递

时间:2021-04-24 07:38:13

I have some difficulties for using Ruby block, passing in a method.

我在使用Ruby块传递方法时遇到了一些困难。

As in the following case, I would like to display each element of @array, from Box instance (using .each method):

如以下情况,我想从Box实例(使用.each方法)显示@array的每个元素:

class Box
  def initialize
    @array = [:foo, :bar]
  end

  def each(&block)
    # well, hm..
  end
end

a = Box.new
a.each { |element| puts element }

2 个解决方案

#1


6  

You really just need to delegate to the each method on @array and pass it the block. Additionally, you can include the Enumerable mix-in to gain access to the methods it provides (e.g. map, inject, etc...):

你只需要将它委托给@array上的每个方法并将它传递给block。此外,您还可以包含可枚举的mix-in来访问它提供的方法(例如map、inject等):

class Box
  include Enumerable

  def initialize
    @array = [:foo, :bar]
  end

  def each(&block)
    @array.each(&block)
  end
end

More information on the Enumerable module is available in the documentation.

有关可枚举模块的更多信息可以在文档中找到。

#2


1  

For this simple example, you actually aren't required to pass the block explicitly:

对于这个简单的示例,实际上不需要显式地传递block:

def each                  
  @array.each{|e| yield e}
end

Passing the block (which is a Proc object) explicitly allows you to test it for things, like the number of arguments that it expects:

显式地传递block (Proc对象)允许您对它进行测试,比如它期望的参数数量:

class Box
  ...    
  def each(&block)
    @array.each do |e|
      case block.arity
      when 0
        yield
      when 1
        yield e
      when 2
        yield e, :baz
      else
        yield
      end
    end
  end
end

a = Box.new
a.each { puts "nothing" }              # displays "nothing, nothing"
a.each { |e| puts e }                  # displays "foo, bar"
a.each { |e1, e2| puts "#{e1} #{e2}" } # displays "foo baz, bar baz" 

#1


6  

You really just need to delegate to the each method on @array and pass it the block. Additionally, you can include the Enumerable mix-in to gain access to the methods it provides (e.g. map, inject, etc...):

你只需要将它委托给@array上的每个方法并将它传递给block。此外,您还可以包含可枚举的mix-in来访问它提供的方法(例如map、inject等):

class Box
  include Enumerable

  def initialize
    @array = [:foo, :bar]
  end

  def each(&block)
    @array.each(&block)
  end
end

More information on the Enumerable module is available in the documentation.

有关可枚举模块的更多信息可以在文档中找到。

#2


1  

For this simple example, you actually aren't required to pass the block explicitly:

对于这个简单的示例,实际上不需要显式地传递block:

def each                  
  @array.each{|e| yield e}
end

Passing the block (which is a Proc object) explicitly allows you to test it for things, like the number of arguments that it expects:

显式地传递block (Proc对象)允许您对它进行测试,比如它期望的参数数量:

class Box
  ...    
  def each(&block)
    @array.each do |e|
      case block.arity
      when 0
        yield
      when 1
        yield e
      when 2
        yield e, :baz
      else
        yield
      end
    end
  end
end

a = Box.new
a.each { puts "nothing" }              # displays "nothing, nothing"
a.each { |e| puts e }                  # displays "foo, bar"
a.each { |e1, e2| puts "#{e1} #{e2}" } # displays "foo baz, bar baz"