我知道我可以使用define_method
動態地定義一個類的方法,並且我使用塊的arity指定了這個方法所需的參數。我可以動態地定義一個帶塊的Ruby方法嗎?
我想動態定義一個方法,它既接受可選參數又接受一個塊。在Ruby 1.9中,這很容易,因爲現在允許將塊傳遞給塊。
不幸的是,Ruby 1.8中不允許這樣,所以下面將不工作:
#Ruby 1.8
class X
define_method :foo do |bar, &baz|
puts bar
baz.call if block_given?
end
end
x = X.new
x.foo("foo") { puts "called!"} #=> LocalJumpError: no block given
與yield
更換明確block.call
沒有任何解決問題。
不幸的是,升級到Ruby 1.9並不適合我。這是一個難以解決的問題,還是存在解決方法?
我需要傳遞一個字符串到class_eval?我不能通過一個塊,這將保持範圍界定? – andrewdotnich
@andrewdotnich好吧,那麼你又回到瞭如何動態定義方法的問題。 –
https://github.com/agrimm/zombie-chaser/blob/master/lib/zombie-chaser/chaser.rb有一個例子和一個鏈接到文件中間的博客文章。 –