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

时间:2016-01-10 06:15:32

标签: ruby indentation

我非常喜欢这种缩进风格:

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 }
        ^

是作者想要的还是一个奇怪的错误?

2 个答案:

答案 0 :(得分:2)

您可以为每一行添加\

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

答案 1 :(得分:1)

在换行符之前移动逗号,ruby再次开心:

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

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