LoginSignup
3
2

More than 5 years have passed since last update.

Integer#downtoの引数にブロックを渡す際の{...}とdo...endの挙動の違い

Last updated at Posted at 2015-01-16

メソッドにブロックを渡すときの、{...}do...endの挙動の違いについてまとめてみた。

downtoメソッドの引数に数値のみ渡す

ブロックを渡さなければ括弧を明示していても、省略しても同じ。

括弧を明示
pry(main)> 3.downto(0)
=> #<Enumerator: ...>
括弧を省略
pry(main)> 3.downto 0
=> #<Enumerator: ...>

downtoメソッドの引数に数値とブロックを渡す

ブロックを渡すと括弧を明示しているか、省略しているかで結果が変わる。
以下④では、括弧を省略して{...}でdowntoメソッドにブロックを渡そうとしているが文法エラーになっている。
{...}の方がdo...endより結合度が強い。
そのため④では0に対してブロックを渡すことになってしまい、エラーが出ている。

①括弧を明示+{...}
pry(main)> 3.downto(0) {|i| puts i}
3
2
1
0
=> 3
②括弧を明示+do…end
pry(main)> 3.downto(0) do |i| puts i end
3
2
1
0
=> 3
③括弧を省略+do…end
pry(main)> 3.downto 0 do |i| puts i end
3
2
1
0
=> 3
④括弧を省略+{…}
pry(main)> 3.downto 0 {|i| puts i}
SyntaxError: unexpected '{', expecting end-of-input
3.downto 0 {|i| puts i}
            ^

参考リンク

instance method Integer#downto(英語)

downto(limit) {|i| block } → self 
downto(limit) → an_enumerator

Iterates the given block, passing decreasing values from int down to and including limit.
If no block is given, an Enumerator is returned instead.

downto
5.downto(1) { |n| print n, ".. " }
print "  Liftoff!\n"
#=> "5.. 4.. 3.. 2.. 1..   Liftoff!"

instance method Integer#downto(日本語)

downto(min) {|n| ... } -> self
downto(min) -> Enumerator

self から min まで 1 ずつ減らしながらブロックを繰り返し実行します。 self < min であれば何もしません。

[PARAM] min:
数値
[RETURN]
self を返します。
[SEE_ALSO] Integer#upto, Numeric#step, Integer#times

ブロックをdo…endで書くか{…}で書くかにより挙動が変わる例

詳しくまとまっているので、挙動の違いの詳細についてはこちらを見た方がよいです

参考にしたツイート

以下のツイートにインスパイアされて記事を書きました。
参考: 武田哲也さんのRuby技術者認定試験受験記

3
2
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
3
2