Dmy*_*iak 42 ruby coding-style ruby-on-rails
这有什么约定?
我使用下面的样式,但不确定它是首选的样式,因为如果我最后错过了一个点,我可能会遇到很多问题而没有意识到这一点.
query = reservations_scope.for_company(current_company).joins{property.development}.
group{property.development.id}.
group{property.development.name}.
group{property.number}.
group{created_at}.
group{price}.
group{reservation_path}.
group{company_id}.
group{user_id}.
group{fee_paid_date}.
group{contract_exchanged_date}.
group{deposit_paid_date}.
group{cancelled_date}.
select_with_reserving_agent_name_for(current_company, [
"developments.id as dev_id",
"developments.name as dev_name",
"properties.number as prop_number",
"reservations.created_at",
"reservations.price",
"reservations.fee_paid_date",
"reservations.contract_exchanged_date",
"reservations.deposit_paid_date",
"reservations.cancelled_date"
]).reorder("developments.name")
query.to_a # ....
Run Code Online (Sandbox Code Playgroud)
那么在多行链接方法的约定是什么?我更喜欢哪一种?
注意:我无法从Ruby编码风格指南中找到一个很好的例子.
Boz*_*sov 51
在Ruby样式指南中实际上有一节:
采用一致的多线方式链式.Ruby社区中有两种流行的样式,它们都被认为是好的 - 领先
.
(选项A)和尾随.
(选项B).
(选项A)在另一行上继续链式方法调用时,请保持
.
第二行.Run Code Online (Sandbox Code Playgroud)# bad - need to consult first line to understand second line one.two.three. four # good - it's immediately clear what's going on the second line one.two.three .four
(选项B)在另一行上继续链式方法调用时,
.
在第一行包含指示表达式继续的方法.Run Code Online (Sandbox Code Playgroud)# bad - need to read ahead to the second line to know that the chain continues one.two.three .four # good - it's immediately clear that the expression continues beyond the first line one.two.three. four
可以在这里找到关于两种替代风格的优点的讨论 .
det*_*zed 32
在Ruby 1.9+中,可以像这样编写:
query = reservations_scope
.for_company(current_company)
.joins{property.development}
.group{property.development.id}
.group{property.development.name}
.group{property.number}
.group{created_at}
.group{price}
.group{reservation_path}
.group{company_id}
.group{user_id}
Run Code Online (Sandbox Code Playgroud)
我认为,更具可读性.
Ror*_*ane 22
以下是四个选项的优缺点列表.其他答案中没有提到其中两个选项.
利弊可以分为独特的和共享的.共享专业人士是另一种选择的独特骗局的逆转.同样,共享缺点是另一个选项的独特专家的反转.对于其他两个选项和缺点,还有一些点是优点.
为了避免重复解释,我只用这一点的摘要来描述每个选项的共享优缺点.有关共享pro或con的完整详细信息,请参见其他选项的独特部分中的反向con或pro的说明.对于两个选项的优点和其他两个选项的优点,我任意选择将完整的解释放在以" .
at line beginning"开头的集合中.
对于较短的列表,隐藏共享的优点和缺点而不是重复它们,请参阅此答案的旧版本.
.
在线端items.get.lazy.
take(10).
force
Run Code Online (Sandbox Code Playgroud)
.
在线开始items.get.lazy
.take(10)
.force
Run Code Online (Sandbox Code Playgroud)
.
或\
.#
注释掉一个连续的行,或者在行之间添加注释,代码将默默地中断.
在行开头,缩进到前一个 .
items.get.lazy
.take(10)
.force
Run Code Online (Sandbox Code Playgroud)
.
或\
..
s 对齐到列中
#
注释掉一个连续的行,或者在行之间添加注释,代码将默默地中断\
在终点,.
在下一行的开头items.get.lazy \
.take(10) \
.force
Run Code Online (Sandbox Code Playgroud)
ros*_*eld 10
我在行尾选择点的原因是它允许您在IRB会话中粘贴代码.此外,如果使用行开头的点,则无法在多行代码中间注释行.这是一个很好的讨论:https://github.com/bbatsov/ruby-style-guide/pull/176
归档时间: |
|
查看次数: |
19235 次 |
最近记录: |