为什么两个用空格分隔的字符串在Ruby中连接在一起? [英] Why do two strings separated by space concatenate in Ruby?
问题描述
这为什么在Ruby中起作用?
Why does this work in Ruby:
"foo" "bar"
# => "foobar"
我不确定为什么将字符串串联起来而不是给出语法错误
I'm unsure as to why the strings were concatenated instead of a syntax error being given.
我很好奇这是否是预期的行为,以及解析器是否负责处理此问题(两个没有运算符的字符串被认为是一个字符串字符串)或语言定义本身正在指定这种行为(隐式concat)。
I'm curious as to whether or not this is expected behavior and whether or not it's something the parser is responsible for wrangling (two strings without operators is considered a single string) or the language definition itself is specifying this behavior (implicit concat).
推荐答案
实施细节可以在<$ c中找到Ruby源代码中的$ c> parse.y 文件。具体来说,此处。
Implementation details can be found in parse.y
file in Ruby source code. Specifically, here.
Ruby string
是 tCHAR
(例如? q
), string1
(例如 q, q或%q {q})或串联的递归定义 string1
和 string
本身的结果,这会导致类似 foo bar的字符串表达式
,'foo' bar
或?f oo'bar'
级联。
A Ruby string
is either a tCHAR
(e.g. ?q
), a string1
(e.g. "q", 'q', or %q{q}), or a recursive definition of the concatenation of string1
and string
itself, which results in string expressions like "foo" "bar"
, 'foo' "bar"
or ?f "oo" 'bar'
being concatenated.
这篇关于为什么两个用空格分隔的字符串在Ruby中连接在一起?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!