我想将参数传递给使用 Define_method 定义的方法,我该怎么做?

有帮助吗?

解决方案

传递给define_method 的块可以包含一些参数。这就是您定义的方法接受参数的方式。当你定义一个方法时,你实际上只是给块起了个昵称并在类中保留对它的引用。参数随块一起提供。所以:

define_method(:say_hi) { |other| puts "Hi, " + other }

其他提示

...如果你想要可选参数

 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 引入了双 splat ** (两颗星)其中(我引用) 做:

Ruby 2.0 引入了关键字参数,** 的作用类似于 *,但用于关键字参数。它返回带有键/值对的哈希。

...当然你也可以在定义方法中使用它:)

 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 合而为一的示例:

 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 运算符是“捕获所有剩余的参数”,而 double splat 是“捕获所有剩余的关键字参数”,因此混合它们会破坏预期的逻辑。(我没有任何参考资料来证明这一点哦!)

2018年8月更新:

文章摘要: https://blog.eq8.eu/til/metaprogramming-ruby-examples.html

除了凯文·康纳的回答之外:块参数不支持与方法参数相同的语义。您不能定义默认参数或块参数。

仅在 Ruby 1.9 中使用支持完整方法参数语义的新替代“stabby lambda”语法修复了此问题。

例子:

# 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' }

在 2.2 中,您现在可以使用关键字参数:https://robots.thoughtbot.com/ruby-2-keyword-arguments

define_method(:method) do |refresh: false|
  ..........
end
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top