错误或功能?奇怪的缩进打破了 ruby 哈希声明

Bug or feature? Weird indentation breaks ruby hashes declaration

我很喜欢这种缩进风格:

hash = 
  [ "bla" => :bla
  , "bli" => :bli
  , "blo" => :blo
  ]

但是Ruby不太喜欢那样。

这没问题:

{ 'a' => 1, 'b' => 2 }
#=> {"a"=>1, "b"=>2}

但是这个:

{ 'a' => 1
, 'b' => 2 }

产量

-:2: syntax error, unexpected ',', expecting '}'
, 'b' => 2 }
 ^
-:2: syntax error, unexpected =>, expecting end-of-input
, 'b' => 2 }
        ^

是作者故意设计的还是奇怪的bug?

把换行前的逗号移一下,ruby又开心了:

hash = 
  [ "bla" => :bla,
    "bli" => :bli,
    "blo" => :blo
  ]

#=> [{"bla"=>:bla, "bli"=>:bli, "blo"=>:blo}]

您可以在每一行添加一个 \

hash = 
  [ "bla" => :bla \
  , "bli" => :bli \
  , "blo" => :blo \
  ]
#=> [{"bla"=>:bla, "bli"=>:bli, "blo"=>:blo}]