Ruby is beautiful for many reasons. One of those is the ability to
return multiple values in a nice syntactic way. These solutions are
not embracing that; they are working with an array instead of with two
values.
Alternatives aside, what do you think of my preferred solution?
Gavin
···
On Wed, Feb 1, 2012 at 11:33 PM, Robert Klemme <shortcutter@googlemail.com> wrote:
Probably because (a,b=foo) in all but the latest version always yields
an Array which is true hence does not make sense as a condition. You
could write if (true) as well.
On first sight it looks like it's probably the best solution for the
given setting. But I cannot get rid of that nagging feeling in the
back of my head that something is totally wrong here. I just can't
put my finger on it.
Maybe it's that we have methods here which are intended to return two
values but actually there is a special case of nil being returned for
the _array_ of two.
I can't test on 1.9.* right now but in 1.8.7 (a,b=nil) is always an
empty array => true. So if the method returns a single value nil you
probably do not get away without assigning that single value to a
variable and go from there. Or you test one of the array elements
only for nil.
Kind regards
robert
···
On Wed, Feb 1, 2012 at 1:42 PM, Gavin Sinclair <gsinclair@gmail.com> wrote:
On Wed, Feb 1, 2012 at 11:33 PM, Robert Klemme > <shortcutter@googlemail.com> wrote:
There are plenty more solutions, for example:
Both contain this, which I think is rather ugly:
week_and_day[0] += @t1.number_of_weeks
Ruby is beautiful for many reasons. One of those is the ability to
return multiple values in a nice syntactic way. These solutions are
not embracing that; they are working with an array instead of with two
values.
Alternatives aside, what do you think of my preferred solution?
ah, portability/compatibility. i guess we're stucked then
thanks Robert.
best regards -botp
···
On Thu, Feb 2, 2012 at 3:47 PM, Robert Klemme <shortcutter@googlemail.com> wrote:
On Thu, Feb 2, 2012 at 3:56 AM, botp <botpena@gmail.com> wrote:
indeed. ruby just checks the last assignment.
if (x,y=(a, b = foo))
p 1
end
SyntaxError: (irb):17: multiple assignment in conditional
but i do not know why it has to do that checking. too much work. and
could probably slow down ruby.
Probably because (a,b=foo) in all but the latest version always yields
an Array which is true hence does not make sense as a condition. You
could write if (true) as well.
ok, i'll take it back, since,
if only the latest version makes sense, then wouldn't that mean it's
not really being used in the earlier versions, and therefore, it
*is* safe to implement it currently, ie remove the checking?
kind regards -botp
···
On Thu, Feb 2, 2012 at 4:57 PM, botp <botpena@gmail.com> wrote:
On Thu, Feb 2, 2012 at 3:47 PM, Robert Klemme > <shortcutter@googlemail.com> wrote:
On Thu, Feb 2, 2012 at 3:56 AM, botp <botpena@gmail.com> wrote:
indeed. ruby just checks the last assignment.
if (x,y=(a, b = foo))
p 1
end
SyntaxError: (irb):17: multiple assignment in conditional
but i do not know why it has to do that checking. too much work. and
could probably slow down ruby.
Probably because (a,b=foo) in all but the latest version always yields
an Array which is true hence does not make sense as a condition. You
could write if (true) as well.
ah, portability/compatibility. i guess we're stucked then
thanks Robert.
best regards -botp
Well spotted wrt 1.8.7. That behaviour is contrary to what I expect.
Assignment should always return the RHS, and nil means nil! I'm glad
it was changed.
···
On Thu, Feb 2, 2012 at 12:25 AM, Robert Klemme <shortcutter@googlemail.com> wrote:
I can't test on 1.9.* right now but in 1.8.7 (a,b=nil) is always an
empty array => true. So if the method returns a single value nil you
probably do not get away without assigning that single value to a
variable and go from there. Or you test one of the array elements
only for nil.
I think so. But that still does not make it a brilliant idiom in my
eyes. Granted, the example Gavin presented looks pretty reasonable.
However, I haven't missed this feature in all those years so I am a
tad uncertain how common or uncommon that is.
Kind regards
robert
···
On Thu, Feb 2, 2012 at 10:03 AM, botp <botpena@gmail.com> wrote:
On Thu, Feb 2, 2012 at 4:57 PM, botp <botpena@gmail.com> wrote:
On Thu, Feb 2, 2012 at 3:47 PM, Robert Klemme >> <shortcutter@googlemail.com> wrote:
On Thu, Feb 2, 2012 at 3:56 AM, botp <botpena@gmail.com> wrote:
indeed. ruby just checks the last assignment.
if (x,y=(a, b = foo))
p 1
end
SyntaxError: (irb):17: multiple assignment in conditional
but i do not know why it has to do that checking. too much work. and
could probably slow down ruby.
Probably because (a,b=foo) in all but the latest version always yields
an Array which is true hence does not make sense as a condition. You
could write if (true) as well.
ah, portability/compatibility. i guess we're stucked then
thanks Robert.
best regards -botp
ok, i'll take it back, since,
if only the latest version makes sense, then wouldn't that mean it's
not really being used in the earlier versions, and therefore, it
*is* safe to implement it currently, ie remove the checking?