News for hackers. Stuff that matters.
Re:New Features in Ruby 2.4 by Rafert on Thursday July 21, 2016 @03:09AM (#12135261)
It has a historical reason: https://bugs.ruby-lang.org/issues/10617
Re:New Features in Ruby 2.4 by psyklic on Thursday July 21, 2016 @03:51AM (#12135397)
To summarize* and elaborate: Originally in Ruby, multiple assignment inside a conditional caused a parse error. Why? Because multiple assignment originally always returned an array:

  irb(main): a, b = nil
  => [nil]
In Ruby, non-empty arrays always evaluate to truthy. (The array above has one element, nil.) Hence, multiple assignment inside a conditional would have been meaningless. For this reason, Ruby threw a parse error.

As of Ruby 1.9, multiple assignment in a conditional now returns whatever the right-hand side of the assignment operator evaluates to. So, it makes sense to re-allow multiple assignment inside conditionals and remove the parse error.

Here are some examples illustrating the return value of multiple assignment. The first two are truthy while the third is falsey:

  irb(main): a, b = 8, 7
  => [8, 7]

  irb(main): a, b = 8
  => 8
  (a = 8, b = nil)

  irb(main): a, b = nil
  => nil
  (a = nil, b = nil)
- The above based on user 'bug hit' who attributes Yusuke Endoh, from https://bugs.ruby-lang.org/issues/10617
Re:New Features in Ruby 2.4 by EdHominem on Thursday July 21, 2016 @12:27PM (#12137920)
puts "true" if []

An empty array is still an object, so tests to true. Similarly 0, etc.

The only false values are 'false', of course, and 'nil', the lack of anything.

Re:New Features in Ruby 2.4 by psyklic on Thursday July 21, 2016 @12:41PM (#12138066)
Good catch! If only other languages were as simple :D