LoginSignup
5
5

More than 5 years have passed since last update.

Ruby の Proc とその周辺

Posted at

時々こんな感じで Proc クラスが登場します。

Proc.new
hello_proc = Proc.new {|name| p "Hello #{name}!"}
hello_proc.call 'Andy' # =>"Hello Andy!"

これは proc メソッドや lambda メソッド、「->」を用いても同じことができます。

procメソッド
hello_proc = proc {|name| p "Hello #{name}!" }
hello_proc.call 'Andy' # =>"Hello Andy!"
lambdaメソッド
hello_proc = lambda {|name| p "Hello #{name}!" }
hello_proc.call 'Andy' # =>"Hello Andy!"
「->」
hello_proc = ->(name) {p "Hello #{name}!" }
hello_proc.call 'Andy' # =>"Hello Andy!"

「->」が一番良く見かける方法ですね。

5
5
1

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
5
5