Why is this syntactically value
def x
return 1, 2
end
but this isn’t
def y
1, 2
end
Why is this syntactically value
def x
return 1, 2
end
but this isn’t
def y
1, 2
end
def x
return 1, 2
end
Here, 1 and 2 are “arguments” to the return statement. It can handle
many of them perfectly well.
def y
1, 2
end
This is invalid syntax - Ruby has no comma operator (unlike, say, C),
and it simply doesn’t know what to do with this code. Commas are only
allowed (I hope I won’t miss anything) in multiple assignment, in
method/block argument list, and in “argument lists” of some language
constructs, like return, rescue or throw.
– Matma R.
On Fri, Feb 10, 2012 at 8:16 AM, Ralph S. [email protected]
wrote:
Thursday, February 9, 2012, 1:35:39 PM, you wrote:
So, wouldn’t a more consistent and more understandable way of doing things bedef x
return [1, 2]
enddef y
[1, 2]
end
Do as you please.
Cheers
robert
Thursday, February 9, 2012, 1:35:39 PM, you wrote:
def x
return 1, 2
end
BD> Here, 1 and 2 are “arguments” to the return statement. It can handle
BD> many of them perfectly well.
def y
1, 2
end
BD> This is invalid syntax - Ruby has no comma operator (unlike, say,
C),
BD> and it simply doesn’t know what to do with this code. Commas are
only
BD> allowed (I hope I won’t miss anything) in multiple assignment, in
BD> method/block argument list, and in “argument lists” of some language
BD> constructs, like return, rescue or throw.
Matma,
So, wouldn’t a more consistent and more understandable way of doing
things be
def x
return [1, 2]
end
def y
[1, 2]
end
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs