【Ruby】Methodオブジェクトの小さなサンプルメモ

# 普通の配列の操作
arr = [1,2,3]
result = arr.shift
p result
p arr

# Methodオブジェクトの使用
arr_shift = arr.method(:shift)
result2 = arr_shift.call
p result2
p arr

# 自作クラスでのMethodオブジェクト

class Test
  def test
    p "test"
  end
end

test_method = Test.new.method(:test)
test_method.call
実行結果
1
[2, 3]
2
[3]
"test"
Sign up for free and join this conversation.
Sign Up
If you already have a Qiita account log in.