LoginSignup
0
0

More than 5 years have passed since last update.

メソッドの引数にメソッドの返り値を渡す場合、blockの渡し方に注意する

Last updated at Posted at 2016-10-11

たとえば、 こんなメソッドがあるとします。

def foo(arg)
  puts "#{arg}, #{block_given?}"
end

def bar
  if block_given?
    yield
  else
    'no block'
  end
end

下記のようにブロックの渡し方でfooメソッド側のblockとなったりbarメソッド側になったりします。想定した渡し方になってない場合があるので注意です。

foo bar do
  'block'
end
# => no block, true

foo bar {
  'block'
}
# => block, false

foo(bar do
  'block'
end)
# => block, false
0
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
0
0