Tbpgr Blog

Employee Experience Engineer tbpgr(てぃーびー) のブログ

書籍 Ruby Cookbook | メソッドの参照の取得

パンくず

Ruby Cookbook
メソッドの参照の取得

概要

メソッドの参照の取得

内容

callでメソッドの呼び出し
arityで引数の数を確認できます。

サンプルコード

# encoding: Windows-31J
require "pp"

class Hoge
  def hoge(txt)
    return "#{txt} hoge"
  end
end

hoge = Hoge.new
puts length_method = hoge.method(:hoge)   # => #<Method: Hoge#hoge>
pp length_method.arity                    # => 引数1個
pp length_method.call("hoge")             # => "hoge hoge"

出力

#<Method: Hoge#hoge>
1
"hoge hoge"