What Is New in Ruby 2.4
Ruby 2.4 brings a host of improvements focused on performance, new capabilities, and modernizing the language. Here's a quick overview of the key changes.
| Category | Key Changes |
|---|---|
| Language Features | Top-level return, Binding#irb, Refinements for instance methods |
| Performance | Faster Fixnum and Bignum unification, Regexp match?, String#match?, Thread#wakeup |
| Standard Library | OpenSSL 1.1.0, Updated libraries (RubyGems, RDoc, BigDecimal, etc.) |
| Deprecations | Fixnum & Bignum classes, Dir#exists?, File#exists? |
What new language features were introduced?
Ruby 2.4 added syntactic sugar and debugging aids that make development smoother. The most notable is the ability to return from the top-level of a program.
You can now use return in the main context, which exits the script. The new Binding#irb method drops you into an IRB session at any point in your code, which is fantastic for live debugging.
Refinements also got more powerful. You can now use them on instance methods like instance_method and instance_methods, giving you finer control over monkey-patching scope.
How did performance improve in this release?
This release tackled performance from several angles, with the unification of Fixnum and Bignum into Integer being the headline act. This simplification cleans up the internals and can speed up certain integer operations.
Regexp matching got faster with the new match? method. Unlike match or =~, it avoids creating a MatchData object and backreferences, which reduces overhead for simple truthy checks.
Other optimizations include a faster Thread#wakeup and improvements to the garbage collector and memory allocation. In practice, these micro-optimizations add up to a snappier experience.
What was updated in the standard library?
The standard library received its usual round of updates, with OpenSSL getting a major bump to version 1.1.0. This matters because it supports newer protocols and cryptographic algorithms.
Several bundled libraries were updated, including RubyGems 2.6.8, RDoc 5.0.0, and BigDecimal 1.3.0. The net modules (net-ftp, net-imap, net-smtp) now use the timeout options from the socket, making network timeouts more consistent.
What was deprecated and needs changing?
This release started the process of cleaning up old naming conventions. The Fixnum and Bignum classes are now deprecated and unified under Integer. You should use Integer for all new code.
The methods Dir.exists? and File.exists? are also deprecated in favor of Dir.exist? and File.exist?. The change aligns the method names with other predicate methods like Array#exist?.
FAQ
What happens to my Fixnum and Bignum constants?
They are now aliases to the Integer class. Your existing code will work, but you'll see deprecation warnings. The long-term fix is to replace them with Integer everywhere.
When should I use Regexp#match? instead of =~?
Use match? when you only need a boolean answer for a match. It's faster because it doesn't create MatchData objects or set global variables like $~.
Is the OpenSSL update backwards compatible?
Upgrading to OpenSSL 1.1.0 could have compatibility implications with very old systems or specific ciphers. You should test your application's TLS connections after upgrading.
How do I use the new Binding#irb method?
Just add binding.irb anywhere in your code. When execution hits that line, it will launch an IRB session in your terminal with the current context, similar to binding.pry from Pry.
What's the deal with the top-level return?
It allows a return statement in the main scope to exit the script entirely. Before 2.4, a top-level return would raise a LocalJumpError.