WHAT’S NEW IN RUBY 2.5

in #ruby6 years ago

The Ruby language has increasingly stabilized over the years. The upcoming release of Ruby 2.5 is not going to let us down too. It introduces lots of new features and improvements over the previous version. The first preview was released on 10th October 2017 and the final build will be released on this 25th. This blog dissects into this latest and exciting release and goes through some of the most important changes, we will be writing another article on performance improvement once the 2.5 if officially released.
Added Hash#transform_keys method
Ruby 2.4 added the transform_values method, 2.5 completes it by adding transform_keys thus make it a perfect pair.

Hash#transform_keys can change keys according to the return value of a block

hash = { a: 1, b: 2 }
=> {:a=>1, :b=>2}
hash.transform_keys { |k| k.to_s }
=> {"a"=>1, "b"=>2}
hash
=> {:a=>1, :b=>2}

transform_keys! is a destructive version:

   
hash = { a: 1, b: 2 }
=> {:a=>1, :b=>2}
hash.transform_keys! { |k| k.to_s } 
=> {"a"=>1, "b"=>2}
hash
=> {"a"=>1, "b"=>2}

Array#prepend and Array#append

Ruby 2.5 brings home two new aliases which are so much better than the two of the most used operations in the language. Array#prepend and Array#append are more programmer friendly than the conventional Array#unshift and Array#push. Ruby IS THE language which focuses on the programmers’ happiness primarily after all.

>> a = ["hello"]
=> ["hello"]
>> a.append "world"
=> ["hello", "world"]
>> a.prepend "Hi"
=> ["Hi", "hello", "world"]

Added yield_self method

This method yields the receiver to the given block and returns the output of the last statement in the block which is somewhat similar to the tap method.The only difference is the value that is returned.yield_self method returns the output of the block but tap method returns the receiver itself.

"Hello".yield_self { |obj| obj + " World"}
=> "Hello World"
 
"Hello".tap { |obj| obj + " World" }
 => "Hello"

rescue/else/ensure are allowed inside do/end blocks without begin/end

We could omit the begin/end and not need the extra wrapper for rescue/else/ensure clauses in Ruby 2.5

[1].each do |n|
  n / 0
rescue
  # rescue
else
  # else
ensure
  # ensure
end

String#delete_prefix/delete_suffix

n Ruby 2.4 we used chomp method to remove the suffix ‘world’ from ‘HelloWorld’ and to remove prefix there is no corresponding method for chomp. The solution was to resort to a sub which is using the regular expression for such a simple task.

Ruby 2.5 added new methods to take care of such tasks. Now in order to delete prefix, we can use delete_prefix and to delete suffix we could use chomp. But the method names don’t seem good. So for symmetry delete_suffix was added.

'HelloWorld'.delete_prefix('Hello')
=> "World" 
'HelloWorld'.delete_suffix('World')
=> "Hello"

References

https://github.com/ruby/ruby/blob/v2_5_0/NEWS
https://www.ruby-lang.org/en/news/2017/12/25/ruby-2-5-0-released/

Sort:  

Congratulations @asvenus! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

The Steem community has lost an epic member! Farewell @woflhart!
SteemitBoard - Witness Update
Do not miss the coming Rocky Mountain Steem Meetup and get a new community badge!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!

Coin Marketplace

STEEM 0.30
TRX 0.11
JST 0.034
BTC 66408.70
ETH 3225.39
USDT 1.00
SBD 4.17