为什么两个用空格分隔的字符串在Ruby中连接在一起? [英] Why do two strings separated by space concatenate in Ruby?

查看:79
本文介绍了为什么两个用空格分隔的字符串在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆