Live data from Hacker News

New Features in Ruby 2.4

blog.blockscore.com

11–20 of 76 posts

Re: New Features in Ruby 2.4

#12
> 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 memo

Re: New Features in Ruby 2.4

#14
post #6
post #2

Some 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 :)

Interestingly, assignment inside conditionals is usually avoided because it is easy to accidentally do an assignment (=) rather than a comparison (==). Note that for multiple assignment this mistake can no longer happen:

  irb(main): a, b == 3
  SyntaxError: syntax error, unexpected ==, expecting '='
Should it still be avoided? :)

Re: New Features in Ruby 2.4

#15
post #11

This code doesn't make sense. 123.digits # => [3, 2, 1]

Usually when I've seen digits indexed in a number it occurs from right to left (certainly in binary contexts--bit 0 is the least significant bit and bit 31 is the the most significant bit in a 32 bit integer). So, 123.digits[0] means the least significant digit, 3, whereas 123.digits[2] would give you 1, the most significant digit.

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…

I assume (but don't know) that this is an attempt to avoid returning `nil` for empty collections: `[].inject(:+)` is `nil`, but `[].sum` is `0`.

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

#18
Ruby 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

Re: New Features in Ruby 2.4

#20

Ruby 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

So there is choice of course.

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 $~

Post reply on HN