Ruby代码美化,多行拆分长指令
Posted
技术标签:
【中文标题】Ruby代码美化,多行拆分长指令【英文标题】:Ruby code beautification, split long instructions on multiple lines 【发布时间】:2011-11-13 16:02:23 【问题描述】:我们如何编写以下语句来提高可读性?
Promotion.joins(:category).where(["lft>=? and rgt<=?", c.lft, c.rgt]).joins(:shops).where(:promotions_per_shops => :shop_id => shops_id ).count('id', :distinct => true)
以下内容无法编译
Promotion.joins(:category)
.where(["lft>=? and rgt<=?", c.lft, c.rgt])
.joins(:shops)
.where(:promotions_per_shops => :shop_id => shops_id )
.count('id', :distinct => true)
syntax error, unexpected '.', expecting kEND
.where(["lft>=? and rgt<=?", c.lft, c.rgt])
【问题讨论】:
【参考方案1】:这样做:
Promotion.joins(:category).
where(["lft>=? and rgt<=?", c.lft, c.rgt]).
joins(:shops).
where(:promotions_per_shops => :shop_id => shops_id ).
count('id', :distinct => true)
【讨论】:
【参考方案2】:它应该在 1.9 中编译。在以前的版本中确实是无效的。
【讨论】:
【参考方案3】:也可以这样做
Promotion.joins(:category) \
.where(["lft>=? and rgt<=?", c.lft, c.rgt]) \
.joins(:shops) \
.where(:promotions_per_shops => :shop_id => shops_id ) \
.count('id', :distinct => true)
【讨论】:
“\”这个东西很有帮助!谢谢以上是关于Ruby代码美化,多行拆分长指令的主要内容,如果未能解决你的问题,请参考以下文章
R语言ggplot2可视化为长文本轴标签自动换行美化可视化结构实战:Wrap long text axis labels