我总是在Rails models
的顶端有很多行代码.我正在寻找用标准Ruby风格打破它们的最佳方式的建议.例如,我现在看到的一行是:
delegate :occupation, :location, :picture_url, :homepage_url, :headline, :full_name, :to => :profile, :prefix => true, :allow_nil => true
打破这些长长的方法调用线的传统方式是什么?
我总是在Rails models
的顶端有很多行代码.我正在寻找用标准Ruby风格打破它们的最佳方式的建议.例如,我现在看到的一行是:
delegate :occupation, :location, :picture_url, :homepage_url, :headline, :full_name, :to => :profile, :prefix => true, :allow_nil => true
打破这些长长的方法调用线的传统方式是什么?
大致如下:
delegate :occupation, :location, :picture_url,
:homepage_url, :headline, :full_name,
:to => :profile, :prefix => true, :allow_nil => true
或者,如果您想突出显示选项哈希(合理的事情):
delegate :occupation, :location, :picture_url,
:homepage_url, :headline, :full_name,
:to => :profile, :prefix => true, :allow_nil => true
把这些都放在一行的 idea 让我觉得是个糟糕的 idea ,这意味着你必须滚动一个任意的数量,才能看到授权的内容.Electron 战.
我可能也会把东西排成一行,或者按字母顺序排列.
delegate :full_name, :headline, :homepage_url,
:location, :occupation, :picture_url,
:to => :profile, :prefix => true, :allow_nil => true
如果该文件没有太多/任何其他实质性内容,我可能会将每个方法符号放在自己的行中,以便于编辑.在更大的文件中,我不想占用空间.
我从来没有想过这种事.
我想是的:/
如今,我可能会根据"相似性"对委托方法进行分组,大致如下:
delegate :full_name, :headline,
:location, :occupation,
:homepage_url, picture_url,
to: :profile, prefix: true, allow_nil: true
My jury's hung on 1.9 hash syntax when the value is also a symbol; I think it looks funny. I'm also not sure where I'd indent it to–might lose it anyway during an IDE reformatting, but I kind of like how it looks above if I'm using the new syntax.