Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 委托给类方法_Ruby - Fatal编程技术网

Ruby 委托给类方法

Ruby 委托给类方法,ruby,Ruby,在Ruby中是否可以委托给另一个类的类方法来替换下面的方法 def sugar(param) Klass.a_very_long_method_name(param) end 我已尝试使用可转发的 extend Forwardable def_delegator 'Klass.a_very_long_method_name', :sugar 但它似乎不起作用 我发现自己在很多地方都叫Klass.a\u很长的方法\u名字,我正试图把事情干掉 require 'forwardab

在Ruby中是否可以委托给另一个类的类方法来替换下面的方法

def sugar(param)
  Klass.a_very_long_method_name(param)
end
我已尝试使用可转发的

   extend Forwardable
   def_delegator 'Klass.a_very_long_method_name', :sugar
但它似乎不起作用

我发现自己在很多地方都叫Klass.a\u很长的方法\u名字
,我正试图把事情干掉

require 'forwardable'

class Klass
  def self.a_very_long_method_name(name)
    puts "Hi, #{name}"
  end
end

class Sugar
  extend Forwardable
  def_delegator Klass, :a_very_long_method_name, :verrry_long
end

s = Sugar.new
s.verrry_long "Sue"
  # Hi, Sue

def_delegator Klass中,:a_very_long_method_name,:verrry_long
Klass
是消息转发的接收者,
:a_very_long_method_name
是要转发的消息,
:verrry_long
是消息的别名
:a_very_long_method_name
,谢谢您的回复。享受你的一天。