2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

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

Posted at
# 普通の配列の操作
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"
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?