From de13d68cddef5bb321469bcffb868f27ddd5390e Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sun, 12 Mar 2023 15:12:10 +0900 Subject: style: add .highlight class to pre tag --- .../ruby-then-keyword-and-case-in/index.html | 24 +++++++++++----------- 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'public/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html') diff --git a/public/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html b/public/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html index 1def6b6..9b1df47 100644 --- a/public/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html +++ b/public/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html @@ -65,7 +65,7 @@ 使われることは稀だが、Ruby ではthenがキーワードになっている。次のように使う:

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

-
+            
               # Example:
 
 if x then
@@ -107,7 +107,7 @@ end
               普通 Ruby のコードでthenを書くことはない。なぜか。次のコードを実行してみるとわかる。
             

-
+            
               if true puts 'Hello, World!' end
             
@@ -115,7 +115,7 @@ end
次のような構文エラーが出力される。

-
+            
               20:1: syntax error, unexpected local variable or method, expecting `then' or ';' or '\n'
   if true puts 'Hello, World!' end
   ^~~~
@@ -131,7 +131,7 @@ end
               ポイントは改行がthen(や;) の代わりとなることである。trueの後に改行を入れてみる。
             

-
+            
               if true
 puts 'Hello, World!' end
             
@@ -147,7 +147,7 @@ puts 'Hello, World!' end なぜthen;や改行 (以下 「then等」) が必要なのだろうか。次の例を見てほしい:

-
+            
               if a b end
             
@@ -155,14 +155,14 @@ puts 'Hello, World!' end then;も改行もないのでエラーになるが、これは条件式がどこまで続いているのかわからないためだ。 この例は二通りに解釈できる。

-
+            
               # a という変数かメソッドの評価結果が truthy なら b という変数かメソッドを評価
 if a then
 b
 end
             
-
+            
               # a というメソッドに b という変数かメソッドの評価結果を渡して呼び出し、
 # その結果が truthy なら何もしない
 if a(b) then
@@ -188,7 +188,7 @@ end
               https://github.com/ruby/ruby/blob/221ca0f8281d39f0dfdfe13b2448875384bbf735/parse.y#L3961-L3986
             

-
+            
               p_case_body : keyword_in
   {
   SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
@@ -221,7 +221,7 @@ end
               簡略版:
             

-
+            
               p_case_body : keyword_in p_top_expr then compstmt p_cases
 ;
             
@@ -234,7 +234,7 @@ end これにより、case-whenによる従来の構文と同じように、then等をパターンの後ろに挿入すればよいことがわかった。つまり次の3通りのいずれかになる:

-
+            
               case x
 in 1 then a
 in 2 then b
@@ -261,7 +261,7 @@ end
               ところで、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