RUBY

The Splat Operator


Hi guys, how’s the Code going? .

I’ve been learning Ruby on Rails for a week now & i must confess that it’s been bliss all the way. I’ve built my first app already and I’ ve got some simple tips and tricks i’ll be sharing with you’all overtime. Today, we’ll talk about the Splat Operator.

Let me show you how to use the Splat Operator( * )  in 5 different ways.

1. Create an Array from a string

Result ==> [“prosper”]

2. Convert a Hash to an array

Result ==> [[:name,’prosper’],[:food, ‘eba’],[:sex, ‘male’]]

3. Flatten an Array

Result ==> [ 10,11,12,13,14,15]

4. Assign Multiple variables

Result ==> [1,2,3]

Result ==> 4

5. Convert an Array to Arguments

Result ==> 6

About Babatunde Mustapha