From 9d5ec5e3bc01c6174dea048e118edee579c36565 Mon Sep 17 00:00:00 2001 From: nsfisis Date: Sat, 7 Feb 2026 23:06:23 +0900 Subject: fix(style): fix codeblock style for rouge --- .../ruby-then-keyword-and-case-in/index.html | 204 ++++++++++----------- 1 file changed, 96 insertions(+), 108 deletions(-) (limited to 'services/nuldoc/public/blog/posts/2021-10-02/ruby-then-keyword-and-case-in') diff --git a/services/nuldoc/public/blog/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html b/services/nuldoc/public/blog/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html index d4a3070b..fcda0ecc 100644 --- a/services/nuldoc/public/blog/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html +++ b/services/nuldoc/public/blog/posts/2021-10-02/ruby-then-keyword-and-case-in/index.html @@ -15,7 +15,7 @@ 【Ruby】 then キーワードと case in|REPL: Rest-Eat-Program Loop - +
@@ -103,38 +103,36 @@ 使われることは稀だが、Ruby では then がキーワードになっている。次のように使う:

-
if cond then
-  puts "Y"
-else
-  puts "N"
-end
-
+
if cond then +
puts "Y" +
else +
puts "N" +
end

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

-
# Example:
-
-if x then
-  a
-end
-
-unless x then
-  a
-end
-
-begin
-  a
-rescue then
-  b
-end
-
-case x
-when p then
-  a
-end
-
+
# Example: +
+
if x then +
a +
end +
+
unless x then +
a +
end +
+
begin +
a +
rescue then +
b +
end +
+
case x +
when p then +
a +
end
@@ -143,19 +141,17 @@ 普通 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'
-if true puts 'Hello, World!' end
-        ^~~~
-20:1: syntax error, unexpected `end', expecting end-of-input
-...f true puts 'Hello, World!' end
-
+
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 +
...f true puts 'Hello, World!' end

二つ目のメッセージは無視して一つ目を読むと、then; か改行が来るはずのところ変数だかメソッドだかが現れたことによりエラーとなっているようだ。 @@ -164,9 +160,8 @@ if true puts 'Hello, World!' end ポイントは改行が then (や ;) の代わりとなることである。true の後に改行を入れてみる。

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

無事 Hello, World! と出力されるようになった。 @@ -178,25 +173,22 @@ if true puts 'Hello, World!' end なぜ then; や改行 (以下 「then 等」) が必要なのだろうか。次の例を見てほしい:

-
if a b end
-
+
if a b end

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

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

then 等はこの曖昧性を排除するためにあり、条件式は if から then 等までの間にある、ということを明確にする。 C系の if 後に来る (/) や、Python の :、Rust/Go/Swift などの { も同じ役割を持つ。 @@ -217,41 +209,39 @@ if true puts 'Hello, World!' end

parse.y
-
p_case_body : keyword_in
-{
-  SET_LEX_STATE(EXPR_BEG|EXPR_LABEL);
-  p->command_start = FALSE;
-  $<ctxt>1 = p->ctxt;
-  p->ctxt.in_kwarg = 1;
-  $<tbl>$ = push_pvtbl(p);
-}
-{
-  $<tbl>$ = push_pktbl(p);
-}
-p_top_expr then
-{
-  pop_pktbl(p, $<tbl>3);
-  pop_pvtbl(p, $<tbl>2);
-  p->ctxt.in_kwarg = $<ctxt>1.in_kwarg;
-}
-compstmt
-p_cases
-{
-  /*%%%*/
-  $$ = NEW_IN($4, $7, $8, &@$);
-  /*% %*/
-  /*% ripper: in!($4, $7, escape_Qundef($8)) %*/
-}
-;
-
+
p_case_body : keyword_in +
{ +
SET_LEX_STATE(EXPR_BEG|EXPR_LABEL); +
p->command_start = FALSE; +
$<ctxt>1 = p->ctxt; +
p->ctxt.in_kwarg = 1; +
$<tbl>$ = push_pvtbl(p); +
} +
{ +
$<tbl>$ = push_pktbl(p); +
} +
p_top_expr then +
{ +
pop_pktbl(p, $<tbl>3); +
pop_pvtbl(p, $<tbl>2); +
p->ctxt.in_kwarg = $<ctxt>1.in_kwarg; +
} +
compstmt +
p_cases +
{ +
/*%%%*/ +
$$ = NEW_IN($4, $7, $8, &@$); +
/*% %*/ +
/*% ripper: in!($4, $7, escape_Qundef($8)) %*/ +
} +
;

簡略版:

-
p_case_body : keyword_in p_top_expr then compstmt p_cases
-;
-
+
p_case_body : keyword_in p_top_expr then compstmt p_cases +
;

ここで、keyword_in は文字通り inp_top_expr はいわゆるパターン、thenthen キーワードのことではなく、この記事で then 等と呼んでいるもの、つまり then キーワード、;、改行のいずれかである。 @@ -260,38 +250,36 @@ p_cases これにより、case - when による従来の構文と同じように、then 等をパターンの後ろに挿入すればよいことがわかった。つまり次の3通りのいずれかになる:

-
case x
-in 1 then a
-in 2 then b
-in 3 then c
-end
-
-case x
-in 1
-  a
-in 2
-  b
-in 3
-  c
-end
-
-case x
-in 1; a
-in 2; b
-in 3; c
-end
-
+
case x +
in 1 then a +
in 2 then b +
in 3 then c +
end +
+
case x +
in 1 +
a +
in 2 +
b +
in 3 +
c +
end +
+
case x +
in 1; a +
in 2; b +
in 3; c +
end

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

-
case x
-in 0 then a
-in n if n < 0 then b
-in n then c
-end
-
+
case x +
in 0 then a +
in n if n < 0 then b +
in n then c +
end
-- cgit v1.3-1-g0d28