From 09b8483ed67d3b85e983ef86c34260081975e1cb Mon Sep 17 00:00:00 2001 From: nsfisis Date: Fri, 2 May 2025 06:47:07 +0900 Subject: fix(blog/nuldoc): remove unnecessary "language" attribute --- .../ruby-then-keyword-and-case-in/index.html | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'vhosts/blog/public/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html') diff --git a/vhosts/blog/public/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html b/vhosts/blog/public/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html index aa5b2781..614957e4 100644 --- a/vhosts/blog/public/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html +++ b/vhosts/blog/public/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html @@ -81,7 +81,7 @@

使われることは稀だが、Ruby では then がキーワードになっている。次のように使う:

-
+
if cond then
   puts "Y"
 else
@@ -91,7 +91,7 @@
             

このキーワードが現れうる場所はいくつかあり、ifunlessrescuecase 構文がそれに当たる。 上記のように、何か条件を書いた後 then を置き、式がそこで終了していることを示すマーカーとして機能する。

-
+
# Example:
 
 if x then
@@ -119,7 +119,7 @@
             

普通 Ruby のコードで then を書くことはない。なぜか。次のコードを実行してみるとわかる。

-
+
if true puts 'Hello, World!' end

@@ -138,7 +138,7 @@

ポイントは改行が then (や ;) の代わりとなることである。true の後に改行を入れてみる。

-
+
if true
 puts 'Hello, World!' end
@@ -151,19 +151,19 @@

なぜ then; や改行 (以下 「then 等」) が必要なのだろうか。次の例を見てほしい:

-
+
if a b end

then; も改行もないのでエラーになるが、これは条件式がどこまで続いているのかわからないためだ。 この例は二通りに解釈できる。

-
+
# a という変数かメソッドの評価結果が truthy なら b という変数かメソッドを評価
 if a then
 b
 end
-
+
# a というメソッドに b という変数かメソッドの評価結果を渡して呼び出し、
 # その結果が truthy なら何もしない
 if a(b) then
@@ -184,7 +184,7 @@
             

https://github.com/ruby/ruby/blob/221ca0f8281d39f0dfdfe13b2448875384bbf735/parse.y#L3961-L3986

-
+
p_case_body : keyword_in
 {
   SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
@@ -215,7 +215,7 @@
             

簡略版:

-
+
p_case_body : keyword_in p_top_expr then compstmt p_cases
 ;
@@ -225,7 +225,7 @@

これにより、case - when による従来の構文と同じように、then 等をパターンの後ろに挿入すればよいことがわかった。つまり次の3通りのいずれかになる:

-
+
case x
 in 1 then a
 in 2 then b
@@ -250,7 +250,7 @@
             

ところで、p_top_expr には if による guard clause が書けるので、その場合は if - then と似たような見た目になる。

-
+
case x
 in 0 then a
 in n if n < 0 then b
-- 
cgit v1.2.3-70-g09d2