You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
According to the Ruby Version Policy, Ruby will release a new MINOR version every Christmas as a Christmas gift, and so we are expecting 2.3.0 this year. This release brings many new features and improvements and this post helps you catch up on the changes.
According to the Ruby Version Policy, Ruby will release a new MINOR version every Christmas as a Christmas gift, and so we are expecting 2.3.0 this year. This release brings many new features and improvements and this post helps you catch up on the changes.
General
Did you mean gem is now bundled by default
Have you ever made a typo, this gem shows the possible corrections for errors like
NoMethodError
andNameError
:Safe operator (
&.
)This new operator helps to avoid method invocation on
nil
.Consider this:
Can now can be reduced to:
Without method name will raise
SyntaxError
:Only invoked when needed:
Useful for attribute assignment too:
Benchmark
Trivia: Matz also calls this the "lonely operator" in his RubyConf 2015 keynote RubyConf 2015 (a person sitting alone and looking at a dot).
Further Reading:
Frozen String Literal Pragma
Add this Magic Comment to the top of a file, and all Strings in the file will then be frozen by default.
This gem can help to add magic comments to all your ruby files.
Alternatively, you can also:
--enable=frozen-string-literal
or--disable=frozen-string-literal
$ RUBYOPT="--enable=frozen-string-literal" ruby
Known Issue:
Related Issue:
Further Reading:
String
<<~
Indented heredocsquish
Active Support's
String#squish
andString#squish!
has been ported to Ruby 2.3.Module
#deprecate_constant
Deppbot::GITHUB_URL
will throw a warning:warning: constant Deppbot::GITHUB_URL is deprecated
.Numeric
#positive?
Benchmark
#negative?
Benchmark
Array
#dig
Faster than
foo[0] && foo[0][0] && foo[0][0][0]
Benchmark
#bsearch_index
While
Array#bsearch
returns a match in sorted array:Array#bsearch_index
returns the index instead:Please note that
#bsearch
and#bsearch_index
only works for sorted array.Struct
#dig
OpenStruct
OpenStruct is now 3X-10X faster in Ruby 2.3 than it was in earlier versions of Ruby.
If you are using
Hashie
in any of your libraries (especially API wrappers), now is a good time to switch back toOpenStruct
.Hash
#dig
Faster than
info[:matz] && info[:matz][:address] && info[:matz][:address][:street]
Benchmark
#>
,#<
,#>=
,#<=
Check if a hash's size is larger / smaller than the other hash, or if a hash is a subset (or equals to) of the other hash.
Check this spec on ruby/rubyspec to learn more.
Further Reading:
Note that there isn't
Hash#<=>
.#fetch_values
Extract many values from a Hash (
fetch_values
is similar tovalues_at
):However,
fetch_values will throw
KeyError` when a given key is not found in the hash:Benchmark
#to_proc
Enumerable
#grep_v
This method returns the inverse of
Enumerable#grep
. Do not confuse this with the$ grep -v
command.#chunk_while
The positive form of
Enumerable#slice_when
:Additional Resources
Thanks for reading!
@JuanitoFatas ✏️ Jolly Good Code
About Jolly Good Code
We specialise in Agile practices and Ruby, and we love contributing to open source.
Speak to us about your next big idea, or check out our projects.
The text was updated successfully, but these errors were encountered: