web-dev-qa-db-ja.com

メソッドへの参照を取得するにはどうすればよいですか?

Rubyでオブジェクトのメソッドへの参照を取得することは可能ですか(これがprocs/lambdasなしで実行できるかどうか知りたいです)、たとえば、次のコードを検討してください:


class X
  def initialize
    @map = {}
    setup_map
  end

  private
  def setup_map
    # @map["a"] = get reference to a method
    # @map["b"] = get reference to b method
    # @map["c"] = get referebce to c method
  end

  public
  def call(a)
    @map["a"](a) if a > 10
    @map["b"](a) if a > 20
    @map["c"](a) if a > 30
  end

  def a(arg)
     puts "a was called with #{arg}"
  end

  def b(arg)
     puts "b was called with #{arg}"
  end

  def c(arg)
    puts "c was called with #{arg}"
  end
end

そのようなことをすることは可能ですか?サブクラス化することでA、B、Cの動作を変更できるようにしたいので、procs/lambdasを避けたいと思います。

32
Geo

あなたが欲しいObject#method

---------------------------------------------------------- Object#method
     obj.method(sym)    => method
------------------------------------------------------------------------
     Looks up the named method as a receiver in obj, returning a Method 
     object (or raising NameError). The Method object acts as a closure 
     in obj's object instance, so instance variables and the value of 
     self remain available.

        class Demo
          def initialize(n)
            @iv = n
          end
          def hello()
            "Hello, @iv = #{@iv}"
          end
        end

        k = Demo.new(99)
        m = k.method(:hello)
        m.call   #=> "Hello, @iv = 99"

        l = Demo.new('Fred')
        m = l.method("hello")
        m.call   #=> "Hello, @iv = Fred"

これで、コードは次のようになります。

private
def setup_map
  @map = {
    'a' => method(:a),
    'b' => method(:b),
    'c' => method(:c)
  }
  # or, more succinctly
  # @map = Hash.new { |_map,name| _map[name] = method(name.to_sym) }
end

public
def call(arg)
  @map["a"][arg] if arg > 10
  @map["b"][arg] if arg > 20
  @map["c"][arg] if arg > 30
end
46
rampion

サブクラスの動作を変更する機能を維持しながら、ラムダを使用してこれを行うことができます。

class X
  def initialize
    @map = {}
    setup_map
  end

  private
  def setup_map
    @map["a"] = lambda { |a| a(a) }
    @map["b"] = lambda { |a| b(a) }
    @map["c"] = lambda { |a| c(a) }
  end

  public
  def call(a)
    @map["a"].call(a) if a > 10
    @map["b"].call(a) if a > 20
    @map["c"].call(a) if a > 30
  end

  def a(arg)
     puts "a was called with #{arg}"
  end

  def b(arg)
     puts "b was called with #{arg}"
  end

  def c(arg)
    puts "c was called with #{arg}"
  end
end
4
Zach Langley

Rubyメソッドは一流のオブジェクトではありません。メッセージパッシングでOOを実装します。

class X
  def call(a)
    self.send(:a, a) if a > 10
    self.send(:b, a) if a > 20
    self.send(:c, a) if a > 30
  end

  def a(arg)
     puts "a was called with #{arg}"
  end

  def b(arg)
     puts "b was called with #{arg}"
  end

  def c(arg)
    puts "c was called with #{arg}"
  end
end

または、直接電話してください。

def call(a)
  self.a(a) if a > 10
  self.b(a) if a > 20
  self.c(a) if a > 30
end
1
Ken

object.method(:method_name)によってメソッドへの参照を取得できます。

例:systemメソッドへの参照を取得します。

m = self.method(:system)
m.call('ls)
0
yask