Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在调用链中间添加注释 我看到Ruby在我试图在调用链中间添加注释时抛出解析错误: Tag.joins(:taggings) # Wanted to add comment here cause it's long # and takes multiple lines .where(...) .where(...) .where(...) # And here .group(...) .order(...)_Ruby_Comments - Fatal编程技术网

在调用链中间添加注释 我看到Ruby在我试图在调用链中间添加注释时抛出解析错误: Tag.joins(:taggings) # Wanted to add comment here cause it's long # and takes multiple lines .where(...) .where(...) .where(...) # And here .group(...) .order(...)

在调用链中间添加注释 我看到Ruby在我试图在调用链中间添加注释时抛出解析错误: Tag.joins(:taggings) # Wanted to add comment here cause it's long # and takes multiple lines .where(...) .where(...) .where(...) # And here .group(...) .order(...),ruby,comments,Ruby,Comments,更改此结构: Tag.joins(:taggings). # Comment 1 # Comment 2 where(...). where(...). where(...). # And here group(...). order(...) 方法末尾的点将指示解析器表达式尚未结束,逻辑方法链将到达。嗯,不要这样做?相反,使用方法将事物分解为具有描述性名称的较小逻辑片段: def add_that_long_thing_to(query)

更改此结构:

Tag.joins(:taggings).
   # Comment 1
   # Comment 2
   where(...).
   where(...).
   where(...).
   # And here
   group(...).
   order(...)

方法
末尾的点将指示解析器表达式尚未结束,逻辑方法链将到达。

嗯,不要这样做?相反,使用方法将事物分解为具有描述性名称的较小逻辑片段:

def add_that_long_thing_to(query)
  # Wanted to add comment here cause it's long 
  # and takes multiple lines
  query.where(...)
       .where(...)
       .where(...)
end

def add_grouping_and_ordering_to_query)
  # And here
  query.group(...)
       .order(...)
end

query = Tag.joins(:taggings)
query = add_that_long_thing_to(query)
query = add_grouping_and_ordering_to(query)

当然,这些名称在现实生活中更为合理,但我们不知道任何代码实际上在做什么,所以我不得不编造一些东西。

不仅仅是解析器,它也是程序员的一个提示,他们可能不希望语句在(多行)注释后继续。在Rails中,您通常会使用。@Stefan当然可以,但如果只在一个地方使用范围,则范围会有点多。@muistooshort:您的示例在语法上是无效的。Ruby将考虑语句在<代码>查询之后结束。在哪里(…)< /代码>。迫使Ruby认识到下一行中的
.where
也属于这个表达式的一种可能性是将所有内容都用括号括起来。这也有助于读者理解整个内容是一个表达式。@user1934428:不,这是完全有效的Ruby。@user1934428 Ruby过去要求在行尾加上点,但后来改为允许在下一行开头加上点,因为(AFAIK)它比把标点符号隐藏在右边更可读。