tags:

views:

84

answers:

2

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
+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
Weird. I don't know Ruby, but it sounds like how lambdas behave in other languages.
Mark
@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
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
@AboutRuby : I agree, but we'l get used to it..
Bragboy
@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ć