【问题标题】:How do you pass arguments to define_method?你如何将参数传递给define_method?
【发布时间】:2010-09-10 12:27:03
【问题描述】:

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

【问题讨论】:

    标签: ruby metaprogramming


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

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

       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 **(两颗星),其中 (I quote) 做到了:

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

      2018 年 8 月更新:

      总结文章:https://blog.eq8.eu/til/metaprogramming-ruby-examples.html

      【讨论】:

      • 有趣——特别是第四块:它在 1.8.7 上确实有效!第一个块在 1.8.7 中不起作用,第二个块有错字(应该是 a.foo 1 而不是 foo 1)。谢谢!
      • 感谢反馈,错字已修复,...在 ruby​​ 1.9.3 和 1.9.2 上,所有示例都有效,我也很肯定在 1.9.1 上(但没有尝试)
      • 我将此答案与stackoverflow.com/questions/4470108/… 处接受的答案结合起来,以计算如何在运行时覆盖(而不是覆盖)采用可选参数和块的方法,并且仍然能够调用原始方法参数和块。啊,红宝石。具体来说,我需要在我的开发环境中覆盖 Savon::Client.request,以便对只能在生产环境中访问的主机进行单个 API 调用。干杯!
      【解决方案3】:

      除了 Kevin Conner 的回答:块参数不支持与方法参数相同的语义。您不能定义默认参数或块参数。

      这仅在 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' }
      

      【讨论】:

      • 实际上,我相信define_method 上的块参数确实支持splat,它也可以提供一种循环方式来定义默认参数。
      【解决方案4】:

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

      define_method(:method) do |refresh: false|
        ..........
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-12
        • 2018-10-22
        • 1970-01-01
        • 1970-01-01
        • 2020-10-09
        • 2012-12-21
        • 2012-01-07
        • 2014-05-03
        相关资源
        最近更新 更多