0
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 3 years have passed since last update.

[Ruby] yieldがあるのにblock引数がない

Posted at

こういう関数

enumerable.rb
module Enumerable
  #省略
  def all?(*several_variants)
    yield to_enum.next; __unknown
  end
end

all?はこんな感じで使う

sample.rb
%w[ant bear cat].all? { |word| word.length >= 3 } #=> true

引数に&blockがあるわけでもないのに、なんで渡せるのか?
それはメソッド中にyieldがあればブロック引数は省略できるからです。

ただ、メソッドの中を見ないとブロックを渡せるかわからなくなるので、省略するかは考える必要がありそうです。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?