Rails:选项哈希的适当缩进是否存在社区标准?

时间:2015-05-08 17:25:21

标签: ruby-on-rails

我注意到许多开发人员都非常小​​心地将代码行保留为尽可能少的字符。考虑到这一点,Rails社区的选项哈希是否比其他格式更广泛使用?这个清单并非包罗万象,最有可能。

所有一行:

@user = User.create(:user, firstname: 'Larry', lastname: 'Jones', position: 'Beekeeper', favorite_movie: 'Wicker Man', favorite_team: 'Hornets')

列出对象后:

@user = User.create(:user, firstname:      'Larry', 
                           lastname:       'Jones', 
                           position:       'Beekeeper', 
                           favorite_movie: 'Wicker Man', 
                           favorite_team:  'Hornets')

对象下的列表:

@user = User.create(:user, 
                    firstname:      'Larry', 
                    lastname:       'Jones', 
                    position:       'Beekeeper', 
                    favorite_movie: 'Wicker Man', 
                    favorite_team:  'Hornets')

缩进较少的列表:

@user = User.create(:user, 
          firstname:      'Larry', 
          lastname:       'Jones', 
          position:       'Beekeeper', 
          favorite_movie: 'Wicker Man', 
          favorite_team:  'Hornets')

1 个答案:

答案 0 :(得分:1)

如果您安装RuboCop,您可以获取代码。 lint包含基于Ruby样式指南的缩进建议。

https://github.com/bbatsov/rubocop

https://github.com/bbatsov/ruby-style-guide

您的对象下的列表是RuboCop喜欢的方式,“如果方法调用的参数跨越多行,则将其对齐。”  https://github.com/bbatsov/ruby-style-guide#no-double-indent

# bad (double indent)
def send_mail(source)
  Mailer.deliver(
      to: 'bob@example.com',
      from: 'us@example.com',
      subject: 'Important message',
      body: source.text)
end

# good
def send_mail(source)
  Mailer.deliver(to: 'bob@example.com',
                 from: 'us@example.com',
                 subject: 'Important message',
                 body: source.text)
end

# good (normal indent)
def send_mail(source)
  Mailer.deliver(
    to: 'bob@example.com',
    from: 'us@example.com',
    subject: 'Important message',
    body: source.text
  )
end`