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
1 2 3 |
name = *'prosper' puts name |
Result ==> [“prosper”]
2. Convert a Hash to an array
1 |
details = *{ name: 'prosper', food: 'eba', sex: 'male'} |
Result ==> [[:name,’prosper’],[:food, ‘eba’],[:sex, ‘male’]]
3. Flatten an Array
1 2 3 4 5 |
first_set = [ 10,11,12] second_set = [ *first_set, 13, 14, 15 ] puts second_set |
Result ==> [ 10,11,12,13,14,15]
4. Assign Multiple variables
1 2 3 4 5 |
*first_set, second_set = [1,2,3,4] puts first_set puts second_set |
Result ==> [1,2,3]
Result ==> 4
5. Convert an Array to Arguments
1 2 3 4 5 6 7 |
def multiply( a, b, c) puts a * b * c end multiply( *[1, 2, 3] ) |
Result ==> 6
- SIMPLE PAGINATION IN ANGULARJS - June 20, 2015
- The Splat Operator - June 19, 2015
PHP
Next Post
Re-iterating the Use Of Traits
JAVASCRIPT