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

后端 未结 2 1627
一个人的身影
一个人的身影 2020-12-30 03:48

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 scen

相关标签:
2条回答
  • 2020-12-30 04:29

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

    0 讨论(0)
  • 2020-12-30 04:32

    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.

    0 讨论(0)
提交回复
热议问题