Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the * (asterisk) symbol do near a function argument and how to use that in others scenarios?

I am using Ruby on Rails 3 and I would like to know what means the presence of a * operator near a function argument and to understand its usages in others scenarios.

Example scenario (this method was from the Ruby on Rails 3 framework):

def find(*args)
  return to_a.find { |*block_args| yield(*block_args) } if block_given?

  options = args.extract_options!

  if options.present?
    apply_finder_options(options).find(*args)
  else
    case args.first
    when :first, :last, :all
      send(args.first)
    else
      find_with_ids(*args)
    end
  end
end
like image 407
user502052 Avatar asked Mar 09 '11 23:03

user502052


People also ask

What is the function of asterisk?

The asterisk is a commonly used wildcard symbol that broadens a search by finding words that start with the same letters. Use it with distinctive word stems to retrieve variations of a term with less typing.

What is asterisk argument in Python?

Here single asterisk( * ) is also used in *args. It is used to pass a variable number of arguments to a function, it is mostly used to pass a non-key argument and variable-length argument list.

What is * args in Ruby?

In the code you posted, *args simply indicates that the method accepts a variable number of arguments in an array called args . It could have been called anything you want (following the Ruby naming rules, of course).

What is an asterisk operator?

The asterisk (star) operator is used in Python with more than one meaning attached to it. For numeric data types, * is used as multiplication operator >>> a=10;b=20 >>> a*b 200 >>> a=1.5; b=2.5; >>> a*b 3.75 >>> a=2+3j; b=3+2j >>> a*b 13j.


2 Answers

This is the splat operator, which comes from ruby (and is thus not rails specific). It can be applied in two ways depending on where it is used:

  • to "pack" a number of arguments into an array
  • to split up an array into an argument list

In your function, you see the splat operator used in the function definition. The result is that the function accepts any number of arguments. The complete argument list will be put into args as an array.

def foo(*args)
  args.each_with_index{ |arg, i| puts "#{i+1}. #{arg}" }
end

foo("a", "b", "c")
# 1. a   <== this is the output
# 2. b
# 3. c

The second variant would be when you consider the following method:

def bar(a, b, c)
  a + b + c
end

It requires exactly three arguments. You can now call this method like follows

my_array = [1, 2, 3]
bar(*my_array)
# returns 6

The splat applied in this case to the array will split it and pass each element of the array as an individual parameter to the method. You could do the same even by calling foo:

foo(*my_array)
# 1. 1   <== this is the output
# 2. 2
# 3. 3

As you can see in your example method, these rules do apply to block parameters in the same way.

like image 152
Holger Just Avatar answered Sep 21 '22 09:09

Holger Just


This is a splat argument, which basically means that any 'extra' arguments passed to the method will all be assigned to *args.

like image 30
TheSeparateFirst Avatar answered Sep 24 '22 09:09

TheSeparateFirst