vendredi 10 février 2017

What is the standalone splat operator used for in Ruby?

Vote count: 0

I just came across this example where the splat operator is used by itself in a method definition:

def print_pair(a,b,*)
  puts "#{a} and #{b}"
end

print_pair 1,2,3,:cake,7
#=> 1 and 2

It is clear what and why you would use it in a context like so:

def arguments_and_opts(*args, opts)
  puts "arguments: #{args} options: #{opts}"
end

arguments_and_opts(1,2,3, a: 5)
#=> arguments: [1, 2, 3] options: {:a=>5}

But why and how would you use it in the first example? Since it is defined in the Ruby specs there must be a usecase for it?

asked 25 secs ago

Let's block ads! (Why?)



What is the standalone splat operator used for in Ruby?

Aucun commentaire:

Enregistrer un commentaire