What's new in Ruby 2.4?
It’s Christmas day, and following the tradition of the last few years, the Ruby core team have released a new Ruby version today. I’ll summarize some of the interesting new features in Ruby 2.4 here.
Previous: Ruby 2.3.
Bignum have been unified into
So far, we’ve had two classes for storing integers -
Fixnum for small integers, and
Bignum for numbers outside this range.
However, these are implementation details
that programmers don’t need to worry about while writing code.
These two classes have been replaced by a single
Integer was a superclass of these two classes,
but now both
Bignum are aliases to
- Feature #12005: Unify Fixnum and Bignum into Integer
- Slides by Tanaka Akira
- Ruby 2.4 unifies Fixnum and Bignum into Integer (BigBinary blog)
Precision for float modifiers
Float methods like
take an optional argument to set precision.
Float#round default behavior remains unchanged
This one isn’t really a change, but this change in default behavior initially made it to one of the preview releases, and was reverted later on.
#round uses round-half-up behavior, ie. 1.5 would be rounded to 2.
The new behavior was to use banker’s rounding, which rounds half to nearest even number.
This might cause bugs in many existing applications which rely on half-up rounding,
so the original default has been retained.
Even though the round-to-nearest-even change was reverted,
new options were introduced in
that allow you to explicitly set what kind of rounding to use.
I’m a big fan of the pry gem for the
that opens a REPL while running your code.
IRB has now introduced this feature,
and ruby now opens a REPL when it encounters the
This method, and the bang version,
remove keys with nil values from the hash.
Applies the block for each value in the hash.
Also provides a
#transform_values! method that modifies the existing hash.
Examples from the docs:
Strings, Symbols and IO
String supports unicode case mappings
Until now, Ruby only performed case conversion on ASCII characters.
upcase, downcase, swapcase, capitalize methods on
have now been extended to work with unicode characters.
Specify string buffer size
String.new now allows a
to specify the size of the buffer.
This will have performance benefits
when the string will be concatenated many times.
Symbol#match now works like
Symbol#match used to return the match position,
String#match returned a
This has been fixed in 2.4 and now both return a
IO#gets and other methods get a chomp flag
You can now add an optional
chomp: true flag to
This new method returns true or false without updating the
$~ global variable.
Since it doesn’t create a
MatchData object or update
it performs better than
Returns a hash representing information about the named captures.
Files and directories
#empty? method was added to
Multiple assignment in conditionals
In Ruby 2.3, you would get a syntax error if you tried multiple assignment in a conditional. This has been changed to a warning instead.
I haven’t mentioned all the new features in Ruby 2.4 here, but if you’re interested in the complete list of changes, take a look at the Ruby 2.4.0 NEWS file.
Here are some more articles covering the new release:
- Ruby 2.4 Released: Faster Hashes, Unified Integers and Better Rounding on the Heroku blog
- What’s new in Ruby 2.4 on the Jobandtalent Engineering team blog
- Behavior changes in Ruby 2.4 on the Wyeworks blog
- 9 New Features in Ruby 2.4 on the Black Bytes blog