I've seen Ruby code in which there are only two double quotes ("") on a line. What does that line do?
+2
A:
Two double quotes represent an literal empty string in Ruby. And in many other languages.
JesperE
2010-09-06 06:52:19
+5
A:
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.
Bragboy
2010-09-06 06:59:29
Weird. I don't know Ruby, but it sounds like how lambdas behave in other languages.
Mark
2010-09-06 07:02:27
@Mark : It was first weird to me as I used Java for many years, but I started to like Ruby very much now! Its pure fun..
Bragboy
2010-09-06 07:22:21
Well, it's not so weird once you know that methods, if they reach their end statement, return the result of the last statement. The two double quotes is not very clear, however. Adding a return statement would at least say you intended to do that. Terseness is not always the best way.
AboutRuby
2010-09-06 07:42:38
@AboutRuby : I agree, but we'l get used to it..
Bragboy
2010-09-06 08:38:17
@AboutRuby: A single literal on a line (specifically, on a last line in a method) shows very clear intention IMHO. It's one of Ruby idioms.
Mladen Jablanović
2010-09-06 08:46:42