New Features in Ruby 2.4
blog.blockscore.com
New Features in Ruby 2.4
1–10 of 76 posts
Re: New Features in Ruby 2.4
#2That being said, I can't help but shake my head at the section about "multiple assignment of conditionals:
"You can now assign multiple variables within a conditional...You probably shouldn’t do that though."
Am I alone and thinking that it's a little bit hypocritical to specifically add functionality to your language if you don't want people to use it? Or is this just a joke that's gone over my head?
Re: New Features in Ruby 2.4
#3`num.to_s.split(//).map(&:to_i)` => `num.digits`
Re: New Features in Ruby 2.4
#4Some of these are pretty amazing, like the `digits` method and the new OptionParser functionality (which seems to be a sort of standard library equivalent to doctopt!). That being said, I can't help but shake my head at the section about "multiple assignment of conditionals: "You can now assign multiple variables within a conditional...You probably shouldn’t do that though." Am I alone and thinking that it's a little…
Re: New Features in Ruby 2.4
#5Some of these are pretty amazing, like the `digits` method and the new OptionParser functionality (which seems to be a sort of standard library equivalent to doctopt!). That being said, I can't help but shake my head at the section about "multiple assignment of conditionals: "You can now assign multiple variables within a conditional...You probably shouldn’t do that though." Am I alone and thinking that it's a little…
Re: New Features in Ruby 2.4
#6Some of these are pretty amazing, like the `digits` method and the new OptionParser functionality (which seems to be a sort of standard library equivalent to doctopt!). That being said, I can't help but shake my head at the section about "multiple assignment of conditionals: "You can now assign multiple variables within a conditional...You probably shouldn’t do that though." Am I alone and thinking that it's a little…
Re: New Features in Ruby 2.4
#7Re: New Features in Ruby 2.4
#8Ruby syntax is just getting goofier and goofier.
Re: New Features in Ruby 2.4
#9Re: New Features in Ruby 2.4
#10Some of these are pretty amazing, like the `digits` method and the new OptionParser functionality (which seems to be a sort of standard library equivalent to doctopt!). That being said, I can't help but shake my head at the section about "multiple assignment of conditionals: "You can now assign multiple variables within a conditional...You probably shouldn’t do that though." Am I alone and thinking that it's a little…
It has a historical reason: https://bugs.ruby-lang.org/issues/10617
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