From a84908b7e8a0e2423afd6b836eccf27a420270b4 Mon Sep 17 00:00:00 2001
From: nsfisis then がキーワードになっている。次のように使う:
if cond then
+ if cond then
puts "Y"
else
puts "N"
@@ -88,7 +88,7 @@
このキーワードが現れうる場所はいくつかあり、if、unless、rescue、case 構文がそれに当たる。 上記のように、何か条件を書いた後 then を置き、式がそこで終了していることを示すマーカーとして機能する。
- # Example:
+ # Example:
if x then
a
@@ -116,13 +116,13 @@
普通 Ruby のコードで then を書くことはない。なぜか。次のコードを実行してみるとわかる。
- if true puts 'Hello, World!' end
+ if true puts 'Hello, World!' end
次のような構文エラーが出力される。
- 20:1: syntax error, unexpected local variable or method, expecting `then' or ';' or '\n'
+ 20:1: syntax error, unexpected local variable or method, expecting `then' or ';' or '\n'
if true puts 'Hello, World!' end
^~~~
20:1: syntax error, unexpected `end', expecting end-of-input
@@ -136,7 +136,7 @@ if true puts 'Hello, World!' end
ポイントは改行が then (や ;) の代わりとなることである。true の後に改行を入れてみる。
- if true
+ if true
puts 'Hello, World!' end
@@ -150,18 +150,18 @@ puts 'Hello, World!' if a b end
+ if a b end
then も ; も改行もないのでエラーになるが、これは条件式がどこまで続いているのかわからないためだ。 この例は二通りに解釈できる。
- # a という変数かメソッドの評価結果が truthy なら b という変数かメソッドを評価
+ # a という変数かメソッドの評価結果が truthy なら b という変数かメソッドを評価
if a then
b
end
- # a というメソッドに b という変数かメソッドの評価結果を渡して呼び出し、
+ # a というメソッドに b という変数かメソッドの評価結果を渡して呼び出し、
# その結果が truthy なら何もしない
if a(b) then
end
@@ -185,7 +185,7 @@ b
https://github.com/ruby/ruby/blob/221ca0f8281d39f0dfdfe13b2448875384bbf735/parse.y#L3961-L3986
- p_case_body : keyword_in
+ p_case_body : keyword_in
{
SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
p->command_start = FALSE;
@@ -216,7 +216,7 @@ p_cases
簡略版:
- p_case_body : keyword_in p_top_expr then compstmt p_cases
+ p_case_body : keyword_in p_top_expr then compstmt p_cases
;
@@ -227,7 +227,7 @@ p_cases
これにより、case - when による従来の構文と同じように、then 等をパターンの後ろに挿入すればよいことがわかった。つまり次の3通りのいずれかになる:
- case x
+ case x
in 1 then a
in 2 then b
in 3 then c
@@ -252,7 +252,7 @@ p_cases
ところで、p_top_expr には if による guard clause が書けるので、その場合は if - then と似たような見た目になる。
- case x
+ case x
in 0 then a
in n if n < 0 then b
in n then c
--
cgit v1.2.3-70-g09d2