“”(两个双引号)在Ruby中做什么?

时间:2022-09-15 15:07:00

I've seen Ruby code in which there are only two double quotes ("") on a line. What does that line do?

我曾见过Ruby代码,其中一行中只有两个双引号(“”)。这条线是做什么的?

2 个解决方案

#1


5  

I assume you might have seen a code like this.

我想你可能见过这样的代码。

def some_method
   #do some operations
   ""
end

In this context, it means that the method is returning an empty string. In Ruby, the last evaluated operation in a method is what is returned from that method. So in this case, it returns an empty string literal.

在此上下文中,它意味着该方法返回一个空字符串。在Ruby中,方法中最后的计算操作是从该方法返回的。在本例中,它返回一个空字符串文字。

#2


2  

Two double quotes represent an literal empty string in Ruby. And in many other languages.

两个双引号表示Ruby中的文字空字符串。还有很多其他语言。

#1


5  

I assume you might have seen a code like this.

我想你可能见过这样的代码。

def some_method
   #do some operations
   ""
end

In this context, it means that the method is returning an empty string. In Ruby, the last evaluated operation in a method is what is returned from that method. So in this case, it returns an empty string literal.

在此上下文中,它意味着该方法返回一个空字符串。在Ruby中,方法中最后的计算操作是从该方法返回的。在本例中,它返回一个空字符串文字。

#2


2  

Two double quotes represent an literal empty string in Ruby. And in many other languages.

两个双引号表示Ruby中的文字空字符串。还有很多其他语言。