4 (of many) awesome additions in Ruby 2.5.1 (and how to install it)

Also a quick aside on Ruby Structs and how to leverage it

Ruby 2.5.1: Released 3/28/2018

Last Tuesday, I attended Fog City Ruby — a monthly meetup in San Francisco featuring three talks involving Ruby and Ruby-related topics. The main talk of the night — Ruby 2.5: A Deep Dive Into Ruby Features by Shannon Skipper from Square — was an excellent rundown of some of the new features implemented into Ruby 2.5. (For his original slides, click here)

Below, I list four notable new features from 2.5 that I learned from his talk.

1. Prepend and Append Arrays

If you ever thought unshift sounded like an awkward name for an array method, that’s because it is.

Ruby 2.5 now lets you add in a value at the beginning of an array by using prepend instead. A lot more readable and rememberable for everyone.

a = [2,3,4] # THE OLD WAY

a.unshift(1) #=> [1,2,3,4] # NEW IN RUBY 2.5

a.prepend(0) #=> [0,1,2,3,4]

You can also append a value to an array instead of pushing or shoveling it in.

b = [1,2,3] # THE OLD WAYS

b << 4 #=> [1,2,3,4]

b.push(5) #=> [1,2,3,4,5] # NEW IN RUBY 2.5

b.append(6) #=> [1,2,3,4,5,6]

2. SecureRandom.alphanumeric

If you ever wanted to create a randomized alphanumeric string, Ruby 2.5 has got you covered. SecureRandom now comes with an alphanumeric method. Just make sure to require 'securerandom' at the top of the file, then call SecureRandom.alphanumeric like below:

require 'securerandom' alnum = SecureRandom.alphanumeric

#=> "gKS5JuHBb2Xu3JtR" alnum.size #=> 16 SecureRandom.alphanumeric(1)

#=> "x" SecureRandom.alphanumeric(2)

#=> "8M" SecureRandom.alphanumeric(20)

#=> "95SqPm8TNixZwonrjZqw"

3. #yield_self

Ruby 2.5 added a new method called yield_self where it yields the given receiver to the given block and returns the output of the last statement in the block.

2.yield_self { |num| num + 3 } #=> 5 "Harry".yield_self { |str| "Hermione, #{str}, and Ron" } #=> "Hermione, Harry, and Ron"

4. Keyword Structs

4a. Aside on what Structs are

Before Shannon introduced us to this new Keyword Structs concept, he asked the group to raise their hands if they’ve ever used Struct before in their Ruby code.

About half the room raised their hands, and I was not one of them. Then I read this blog post and now I know what it is. I’ll summarize here:

Straight from the Ruby docs, a Struct is a convenient way to bundle a number of attributes together by using accessor methods without having to write an explicit class.

To set up a Ruby class, (I will use the cliche Dog class with an instance of Fido here), one might write it the textbook way like so:

class Dog

attr_accessor :name, :breed





end def initialize(name, breed) @name = name @breed = breedend def introduce_self

"Hi, my name is #{name} and I'm a #{breed}."

end end fido = Dog.new('Fido', 'Corgi')

puts fido.introduce_self #=> Hi, my name is Fido and I'm a Corgi.

With Struct , you can greatly abbreviate the process of making a dog introduce oneself by writing the following instead:

Dog = Struct.new(:name, :breed) do

def introduce_self

"Hi, my name is #{name} and I'm a #{breed}."

end

end fido = Dog.new('Fido', 'Corgi')

puts fido.introduce_self #=> Hi, my name is Fido and I'm a Corgi.

No need to type out attr_accessors or initialize methods with instance variables, Struct takes care of all that for you.

4b. Keyword Structs in Ruby 2.5

Back to talking about Ruby 2.5…

Suppose we wanted to create Spider-man using Struct as follows:

Superhero = Struct.new(:name, :real_name) do

def introduce_self

"Hi, I'm #{name}, but my friends call me #{real_name}."

end

end spiderman = Superhero.new("Spiderman", "Peter") puts spiderman.introduce_self

#=> Hi, I’m Spiderman, but my friends call me Peter.

Say three hours later, you wanted to create Batman, but you keep forgetting how the order of the names go. Was it his real name first, or his superhero name first?

Thanks to Ruby 2.5’s keyword structs, you can now create an instance of a Struct using keyword arguments like so:

Superhero = Struct.new(:name, :real_name, :keyword_init => true) do

def introduce_self

"Hi, I'm #{name}, but my friends call me #{real_name}."

end

end batman = Superhero.new(real_name: "Bruce", name: "Batman") puts batman.introduce_self

#=> Hi, I'm Batman, but my friends call me Bruce.

Just note the :keyword_init => true line at the top, and how we are now able to create a new instance of a superhero by specifying its key-value pairs.

How to Install

So how do you get your hands on Ruby 2.5?

First of all, check to see if you already have it. Type in your terminal:

ruby -v

and it should output what version you currently have. Mine was:

ruby 2.3.1p112

I was behind the times.

Get the Ruby Version Manager downloaded into your machine by typing into the terminal:

curl -L https://get.rvm.io | bash -s stable

Then type in:

rvm install ruby-2.5.1

It should take its time downloading it.

Once downloaded, set it as your default go-to Ruby version by typing in:

rvm --default use 2.5.1

Closing Thoughts

The above methods are just a few of several methods that Ruby 2.5 brought in to make your life a little easier. Be sure to check out the official docs and the slides aforementioned for lists of all the performance improvements and a new default list of gems.

Also there’s a pretty excellent blog series on Ruby 2.5. Check that out too as I referenced the series frequently as I wrote this post.

So be sure to update your Ruby and stay current out there!

Thanks for reading! Please feel free to leave a few claps down below!