1
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 1 year has passed since last update.

【Ruby】Procオブジェクトの呼び出し

Last updated at Posted at 2022-03-22
def foo(n)
  n ** n
end

foo = Proc.new { |n|
  n * 4
}

puts foo[2] * 2

この答えは 16 になる。
Procを呼び出すときには、[]もしくは.callで呼び出すことができるらしい。

そのため、この問題ではputs foo[2] * 2としているため、

foo = Proc.new { |n|
  n * 4
}

この処理が走るということらしい。

ちなみに、puts foo 2 * 2であれば、

def foo(n)
  n ** n
end

この処理が走ることになるらしい。
あっているかわからないので、詳しい方コメントいただけたら嬉しいです。

1
0
2

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
1
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?