关于ruby:如何将参数传递给define_method?

关于ruby:如何将参数传递给define_method?

How do you pass arguments to define_method?

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


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

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

...以及是否需要可选参数

1
2
3
4
5
6
7
8
9
10
11
 class Bar
   define_method(:foo) do |arg=nil|                  
     arg                                                                                          
   end  
 end

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

...尽可能多的参数

1
2
3
4
5
6
7
8
9
10
11
12
13
 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']

...的组合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 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]

... 他们全部

1
2
3
4
5
6
7
8
9
10
11
12
 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 introduced keyword arguments, and ** acts like *, but for keyword arguments. It returns a Hash with key / value pairs.

...当然,您也可以在define方法中使用它:)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
 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}

命名属性示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 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
2
 define_method(:foo) do |variable1, variable2,*arg, i_will_not: 'work', **options, &block|
    # ...

要么

1
2
 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


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

仅在Ruby 1.9中使用新的" stabby lambda"语法来修复此问题,该语法支持完整的方法参数语义。

例:

1
2
3
4
5
6
7
8
# 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

1
2
3
define_method(:method) do |refresh: false|
  ..........
end


推荐阅读

    linux命令定义详解?

    linux命令定义详解?,工作,系统,管理,命令,信息,单位,数据,基础,简介,目录,Lin

    linux命令参数大全?

    linux命令参数大全?,系统,工作,管理,命令,信息,基础,工具,网络,标准,百度,lin

    linux命令中参数大全?

    linux命令中参数大全?,网络,工作,信息,命令,状态,服务,情况,地址,管理,系统,L

    linux命令各参数意思?

    linux命令各参数意思?,管理,地址,命令,系统,数据,文件,数字,工作,互动,灵活,L

    linux过滤命令参数?

    linux过滤命令参数?,工具,数据,标准,地址,命令,设备,系统,信息,指令,文件,如

    linux下ls命令参数?

    linux下ls命令参数?,时间,系统,数据,命令,文件,信息,标准,密码,工作,名字,lin

    linux读取命令行参数?

    linux读取命令行参数?,系统,信息,数据,名称,软件,位置,标准,灵活,百度,资料,L

    linux参数设置命令?

    linux参数设置命令?,网络,系统,地址,工作,信息,管理,服务,名称,状态,命令,在l

    linux主要命令及参数?

    linux主要命令及参数?,网络,工作,系统,信息,地址,命令,数字,服务,状态,情况,L

    linux主要命令及参数?

    linux主要命令及参数?,网络,工作,系统,信息,地址,命令,数字,服务,状态,情况,L

    linux命令du参数?

    linux命令du参数?,工作,信息,系统,情况,单位,地址,命令,报告,时间,数据,在LIN

    linux命令参数r与k?

    linux命令参数r与k?,工作,地址,系统,信息,命令,目录,管理,标准,基础,控制台,3

    linuxrm命令参数?

    linuxrm命令参数?,系统,命令,文件,目录,环境,档案,文件夹,终端,参数,子目录,l

    创建自定义命令linux?

    创建自定义命令linux?,工具,状态,命令,系统,代码,标准,数据,位置,电脑,材料,L

    linux自定义命令行?

    linux自定义命令行?,系统,工作,名称,百度,命令,管理,工具,位置,信息,终端,Lin

    linux配置自定义命令?

    linux配置自定义命令?,服务,系统,状态,策略,周期,地方,标准,新增,环境,工具,L

    linux使用命令的方法?

    linux使用命令的方法?,系统,信息,工具,标准,数据,命令,左下角,目录,文件夹,

    自定义命令linux命令?

    自定义命令linux命令?,工具,系统,数据,命令,新增,代理,通信,地方,信息,时间,

    linux命令行定义参数?

    linux命令行定义参数?,系统,信息,名称,实时,命令,百分比,工作,周期,选项,参

    linux命令查询参数?

    linux命令查询参数?,网络,信息,设备,系统,服务,状态,情况,工作,地址,命令,Lin