简体   繁体   English

Ruby单引号和双引号

[英]Ruby single and double quotes

I've recently been coding in Ruby and have come from Python, where single and double quotes made no difference to how the code worked as far as I know. 我最近一直使用Ruby进行编码,并且来自Python,据我所知,单引号和双引号对代码的工作方式没有任何影响。

I moved to Ruby to see how it worked, and to investigate the similarities between Ruby and Python. 我转到Ruby看看它是如何工作的,并调查Ruby和Python之间的相似之处。

I was using single-quoted strings once and noticed this: 我曾使用单引号字符串并注意到这一点:

hello = 'hello'
x = '#{hello} world!'
puts x

It returned '#{hello} world!' 它返回'#{hello} world!' rather than 'hello world!' 而不是'hello world!' .

After noticing this I tried double quotes and the problem was fixed. 注意到这一点后,我尝试了双引号,问题得到解决。 Now I'm not sure why that is. 现在我不确定为什么会这样。

Do single and double quotes change this or is it because of my editor (Sublime text 3)? 单引号和双引号会改变这个还是因为我的编辑器(Sublime text 3)? I'm also using Ruby version 2.0 if it works differently in previous versions. 如果它在以前的版本中工作方式不同,我也使用Ruby 2.0版。

In Ruby, double quotes are interpolated, meaning the code in #{} is evaluated as Ruby. 在Ruby中,双引号被内插,这意味着#{}的代码被评估为Ruby。 Single quotes are treated as literals (meaning the code isn't evaluated). 单引号被视为文字(意味着不评估代码)。

var = "hello"
"#{var} world" #=> "hello world"
'#{var} world' #=> "#{var} world"

For some extra-special magic, Ruby also offers another way to create strings: 对于一些特殊的魔术,Ruby还提供了另一种创建字符串的方法:

%Q() # behaves like double quotes
%q() # behaves like single quotes

For example: 例如:

%Q(#{var} world) #=> "hello world"
%q(#{var} world) #=> "#{var} world"

If you enclose Ruby string in single qoutes, you can't use interpolation. 如果将Ruby字符串括在单个qoutes中,则不能使用插值。 That's how Ruby works. 这就是Ruby的工作方式。

Single-quoted strings don't process escape sequence \\ and they don't do string interpolation. 单引号字符串不处理转义序列\\并且它们不进行字符串插值。 For a better understanding, take a look at String concatenation vs. interpolation 为了更好地理解,请查看字符串连接与插值

To answer your question, you have to use "" when you want to do string interpolation: 要回答您的问题,当您想要进行字符串插值时,必须使用""

name = 'world'
puts "Hello #{name}" # => "Hello world"

Using escape sequence: 使用转义序列:

puts 'Hello\nworld'       # => "Hello\nworld"
puts "Hello\nworld"       # => "Hello
                                world"

You should read the Literals section of the official Ruby documentation. 您应该阅读官方Ruby文档的Literals部分。

It is very concise, so you need to read carefully. 它非常简洁,所以你需要仔细阅读。 But it explains the difference between double-quoted and single-quoted strings, and how they are equivalent to %Q/.../ and %q/.../ respectively. 但它解释了双引号和单引号字符串之间的区别,以及它们如何分别等同于%Q/.../%q/.../

Ruby supports single-quoted string, for many uses like as follow: Ruby支持单引号字符串,用于以下许多用途:

>> 'foo'
=> "foo"
>> 'foo' + 'bar'
=> "foobar"

In above example, those two types of strings are identical. 在上面的例子中,这两种类型的字符串是相同的。 We can use double quote in place of single quote and we will get same output like above example. 我们可以使用双引号代替单引号,我们将获得与上例相同的输出。

As you face problem, while using interpolation in single quoted string because Ruby do not interpolate into single-quoted string. 当你遇到问题时,在单引号字符串中使用插值是因为Ruby不插入单引号字符串。 I am taking one example for more understanding: 我举一个例子来进一步理解:

>> '#{foo} bar'
=> "\#{foo} bar"

Here you can see that return values using double-quoted strings, which requires backslash to escape special characters such as #. 在这里,您可以看到使用双引号字符串返回值,这需要使用反斜杠来转义特殊字符,例如#。

Single quoted string often useful because they are truly literal. 单引号字符串通常很有用,因为它们是真正的文字。

In the string interpolation concept, the essential difference between using single or double quotes is that double quotes allow for escape sequences while single quotes do not. 在字符串插值概念中,使用单引号或双引号之间的本质区别在于双引号允许转义序列,而单引号则不允许。

Let's take an example: 我们来举个例子:

name = "Mike"
puts "Hello #{name} \n How are you?"

The above ruby code with string interpolation will interpolate the variable called name which is written inside brackets with its original value which is Mike . 上面带有字符串插值的ruby代码将插入名为name的变量,该变量写在括号内,其原始值为Mike And it will also print the string How are you? 它还会打印字符串你好吗? in a separate line since we already placed an escape sequence there. 因为我们已经在那里放置了一个转义序列。

Output: 输出:

Hello Mike 
  How are you?

If you do the same with single quotes, it will treat the entire string as a text and it will print as it is including the escape sequence as well. 如果对单引号执行相同操作,它会将整个字符串视为文本,并且它将按原样打印,包括转义序列。

name = Mike'
puts 'Hello #{name} \n How are you'?

Output: 输出:

Hello #{name} \n How are you?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM