1

我想将一个块传递给一个函数,然后使用一些附加参数调用该块,如下所示:

def foo(&block)
  some_array = (1..3).to_a
  x = 7 # Simplified
  result = some_array.map &block # Need some way to pass in 'x' here
end

def a_usage_that_works
  foo do |value|
    value
  end
end

def a_usage_that_doesnt_work
  foo do |value, x|
    x # How do I pass in x?
  end
end

# rspec to demonstrate problem / required result
describe "spike" do
  it "works" do
    a_usage_that_works.should == [1,2,3]
  end
  it "doesn't work" do
    a_usage_that_doesnt_work.should == [7, 7, 7]
  end
end

如何将附加参数传递给块?

4

3 回答 3

2

创建另一个块并从中调用第一个块。

def foo(&block)
  some_array = (1..3).to_a
  x = 7 # Simplified
  result = some_array.map {|elem| block.call(elem, x)}
end
于 2013-10-14T12:40:08.027 回答
1

你通过屈服于它传递给它。

def foo(&block)
  some_array = [1,2,3]
  x = 7
  some_array.map{|el| yield el, x}
end

p foo{|p1, p2| p2} #=>[7,7,7]
p foo{|p1, p2| p1} #=>[1,2,3]
于 2013-10-14T13:35:18.023 回答
0

您可以使用高阶函数来生成简化函数:

假设我们传递给的块foo将接受value, x

天真的策略,使用内联定义x

def foo(&block)
  some_array = (1..3).to_a
  x = 7
  simple_func = proc {|value| block.call(value, x) }
  result = some_array.map &simple_func
end

使用关注点分离的策略:

def get_simple_func(block)
  # This assumes x won't change per iteration.
  # If it can change, you can move the calculation inside the proc.
  # Moving it inside also allows the calculation to depend on "value", in case you want that.
  x = complex_calculation_for_x()
  proc {|value| block.call(value, x) }
end

def foo(&block)
  some_array = (1..3).to_a
  simple_func = get_simple_func(block)
  result = some_array.map &simple_func
end

显然,当是文字值时,您不应该使用它,x因为它会过度设计。但是随着计算x变得越来越复杂,将其分离出来会使代码更具可读性。此外,foo可以专注于将功能应用于some_array.

于 2013-10-14T16:59:46.737 回答