162

define_method を使用して定義されているメソッドに引数を渡したいのですが、どうすればよいですか?

4

4 に答える 4

207

define_method に渡すブロックには、いくつかのパラメーターを含めることができます。これが、定義済みのメソッドが引数を受け入れる方法です。メソッドを定義するときは、実際にはブロックにニックネームを付けて、クラス内でそれへの参照を保持しているだけです。パラメータはブロックに付属しています。そう:

define_method(:say_hi) { |other| puts "Hi, " + other }
于 2008-09-18T03:06:02.257 に答える
101

...オプションのパラメータが必要な場合

 class Bar
   define_method(:foo) do |arg=nil|                  
     arg                                                                                          
   end   
 end

 a = Bar.new
 a.foo
 #=> nil
 a.foo 1
 # => 1

... 必要な数の引数

 class Bar
   define_method(:foo) do |*arg|                  
     arg                                                                                          
   end   
 end

 a = Bar.new
 a.foo
 #=> []
 a.foo 1
 # => [1]
 a.foo 1, 2 , 'AAA'
 # => [1, 2, 'AAA']

...の組み合わせ

 class Bar
   define_method(:foo) do |bubla,*arg|
     p bubla                  
     p arg                                                                                          
   end   
 end

 a = Bar.new
 a.foo
 #=> wrong number of arguments (0 for 1)
 a.foo 1
 # 1
 # []

 a.foo 1, 2 ,3 ,4
 # 1
 # [2,3,4]

...それらすべて

 class Bar
   define_method(:foo) do |variable1, variable2,*arg, &block|  
     p  variable1     
     p  variable2
     p  arg
     p  block.inspect                                                                              
   end   
 end
 a = Bar.new      
 a.foo :one, 'two', :three, 4, 5 do
   'six'
 end

アップデート

Ruby 2.0 では、ダブル スプラット**(2 つの星) が導入されました。

Ruby 2.0 ではキーワード引数が導入され、** は * のように機能しますが、キーワード引数の場合です。キーと値のペアを含むハッシュを返します。

...そしてもちろん、defineメソッドでも使用できます:)

 class Bar 
   define_method(:foo) do |variable1, variable2,*arg,**options, &block|
     p  variable1
     p  variable2
     p  arg
     p  options
     p  block.inspect
   end 
 end 
 a = Bar.new
 a.foo :one, 'two', :three, 4, 5, ruby: 'is awesome', foo: :bar do
   'six'
 end
# :one
# "two"
# [:three, 4, 5]
# {:ruby=>"is awesome", :foo=>:bar}

名前付き属性の例:

 class Bar
   define_method(:foo) do |variable1, color: 'blue', **other_options, &block|
     p  variable1
     p  color
     p  other_options
     p  block.inspect
   end
 end
 a = Bar.new
 a.foo :one, color: 'red', ruby: 'is awesome', foo: :bar do
   'six'
 end
# :one
# "red"
# {:ruby=>"is awesome", :foo=>:bar}

キーワード引数、splat、および double splat を 1 つにまとめた例を作成しようとしていました。

 define_method(:foo) do |variable1, variable2,*arg, i_will_not: 'work', **options, &block|
    # ...

また

 define_method(:foo) do |variable1, variable2, i_will_not: 'work', *arg, **options, &block|
    # ...

...しかし、これは機能しません。制限があるようです。考えてみると、splat 演算子は「残りのすべての引数をキャプチャ」し、ダブル splat は「残りのすべてのキーワード引数をキャプチャ」しているため、それらを混在させると予想されるロジックが壊れます。(この点を証明する参考文献はありません!)

2018 年 8 月の更新:

まとめ記事:https ://blog.eq8.eu/til/metaprogramming-ruby-examples.html

于 2012-06-19T09:48:50.123 に答える
60

Kevin Conner の回答に加えて、ブロック引数はメソッド引数と同じセマンティクスをサポートしていません。デフォルト引数またはブロック引数を定義することはできません。

これは、完全なメソッド引数セマンティクスをサポートする新しい代替「スタビー ラムダ」構文を使用する Ruby 1.9 でのみ修正されます。

例:

# Works
def meth(default = :foo, *splat, &block) puts 'Bar'; end

# Doesn't work
define_method :meth { |default = :foo, *splat, &block| puts 'Bar' }

# This works in Ruby 1.9 (modulo typos, I don't actually have it installed)
define_method :meth, ->(default = :foo, *splat, &block) { puts 'Bar' }
于 2008-09-20T21:02:34.263 に答える