Intro to Splat (*) operator in Ruby.

I usually use following to create a bunch of array elements in Ruby.

arr = (10..50).to_a     #converting a range to an array.

Today I noticed following which performs same operation:

arr = [*10..50]       # splat operator

My reaction was :
“Awesome!!!!”

So, then, one day I took some time to investigate more on this (*) operator.
Following are few which I could come to know.

1) Generally used in “Method definition with variable no. of parameter” (I have used it but don’t know it was implementation of splat operator (^o^) )

def demo(*numbers)
  numbers.each { |num| puts "#{num}" }
end
demo(1,2,3,4)

#output
1
2
3
4

2) Converting an array into list of arguments in method calling.
In this case, the splat converts the array into method arguments.

def demo(arg1, arg2, arg3)
  puts "arg1 is #{arg1}, arg2 is #{arg2} and arg3 is #{arg3}"
end
arr = [10, 20.45, "hello"]
demo *arr

#output
arg1 is 10, arg2 is 20.45 and arg3 is hello

3) Datatype coercion
Splat operator can be used to convert interesting datatype coercion.

3.1) String into Array : Splat can also be used to coerce string values into array.

arr = *"Hello"  #=> ["Hello"]
"Hello".to_a  #=> NoMethodError: undefined method `to_a' for "Hello":String

Note : This will only create array of size 1.

3.2) Range into Array :

arr = *(10..20)     # [10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

Above is similar behavior as ‘arr = (10..20).to_a’

3.3) Hash into an Array:

arr = *{ :a => "111", :b => "222" }    # [[:a, "111"], [:b, "222"]]

Note : Use ‘Hash[*arr.flatten]’ to reverse it.

4) Use in ‘case’ statement :

male = ["ram", "rahul", "karan"]
female = ["kareena", "aish", "juhi", "katerina"]
person = "aish"

case person
when *male
  puts "Male"
when *female
  puts "Female"
end

#output
Female

5) Interesting Array data retrieval:

arr = ["one", "two", "three", "four"]

first, *others = arr         #first = "one", others = ["two", "three", "four"]
*others, last = arr          #others = ["one", "two", "three"] , last = "four"
first, *center, last = arr   #first = "one", center = ["two", "three"], last = "four"  
Advertisements

About budhramgurung

I am a self-motivated experienced Software Engineer with good communication and interpersonal skills and has the ability to work independently, can lead, motivate and influence others and can train and mentor others. Has hands-on experience in RoR since Jan, 2012. I am an open source contributor and holds a Master's Degree in Computer Science and experience working as a Software Engineer since August, 2011.

Posted on September 15, 2012, in ruby and tagged , , , , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: