123.digits # => [3, 2, 1]New Features in Ruby 2.4
11–20 of 76 posts
Re: New Features in Ruby 2.4
#12Why? This doesn't seem necessary, but, more importantly, would be inconsistent with #inject's behaviour (if enforced).
----
http://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-inje...:
If you do not explicitly specify an initial value for memo, then the first element of collection is used as the initial value of memo
Re: New Features in Ruby 2.4
#13Re: New Features in Ruby 2.4
#14Some 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…
Ah well "You probably shouldn’t do that though" is just my opinion as the author. I didn't play a part in these changes. I just like Ruby enough to dig into their changelog :)
irb(main): a, b == 3
SyntaxError: syntax error, unexpected ==, expecting '='
Should it still be avoided? :)Re: New Features in Ruby 2.4
#15This code doesn't make sense. 123.digits # => [3, 2, 1]
Re: New Features in Ruby 2.4
#16> If you are calling #sum on an array of non-integers then you need to provide your own initial value Why? This doesn't seem necessary, but, more importantly, would be inconsistent with #inject's behaviour (if enforced). ---- http://ruby-doc.org/core-2.3.1/Enumerable.html#method-i-inje... : If you do not explicitly specify an initial value for memo, then the first element of collection is used as the initial value of…
Forcing the use of the additive identity every time, rather than starting with the first element of the collection, is a good way of setting people up to get the correct behaviour on empty collections, even if they don't test for that case.
Re: New Features in Ruby 2.4
#17digits method just saved code-golfers 20 characters. `num.to_s.split(//).map(&:to_i)` => `num.digits`
Re: New Features in Ruby 2.4
#18Why does Ruby have 4 different regexp match functions?
Regexp#match?: 2630002.5 i/s Regexp#===: 872217.5 i/s - 3.02x slower Regexp#=~: 859713.0 i/s - 3.06x slower Regexp#match: 539361.3 i/s - 4.88x slower
Re: New Features in Ruby 2.4
#19Re: New Features in Ruby 2.4
#20Ruby 2.4 adds a new #match? method for regular expressions which is three times faster than any Regexp method in Ruby 2.3: Why does Ruby have 4 different regexp match functions? Regexp#match?: 2630002.5 i/s Regexp#===: 872217.5 i/s - 3.02x slower Regexp#=~: 859713.0 i/s - 3.06x slower Regexp#match: 539361.3 i/s - 4.88x slower
match returns match data, and sets the $~ variable
=== returns true or false, setting the $~ variable
=~ returns integer (position) or nil, setting $~
The newest one match? returns a boolean, not setting $~