Lazy Diary @ Hatena Blog

PowerShell / Java / miscellaneous things about software development, Tips & Gochas. CC BY-SA 4.0/Apache License 2.0

多値リターンを別のメソッドの引数とする (Ruby 1.6)

#!/usr/bin/ruby

class A
  def foo(a, b)
    print a.to_s + "/" + b.to_s + "\n"
  end

  def A::bar
    return 100, 200
  end
end

m = A.new.method(:foo).to_proc
m.call(A::bar())

instance.method(:methodname).to_proc で Proc オブジェクトを得て、Proc#call を使えばよい。
弱点: メソッドの属するクラスのコンストラクタには渡せない。
他にいい方法はあるのかな?