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

风流意气都作罢 提交于 2019-12-30 01:51:29

问题


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

回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/5253545/what-does-the-asterisk-symbol-do-near-a-function-argument-and-how-to-use-tha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!