Ruby中使用的是什么?

Jos*_*cha 2 ruby

*在下面的代码和Ruby编程语言中'test'之前做了什么?

  def sample (*test)
       puts "The number of parameters is #{test.length}"
       for i in 0...test.length
          puts "The parameters are #{test[i]}"
       end
    end
    sample "Zara", "6", "F"
    sample "Mac", "36", "M", "MCA"
Run Code Online (Sandbox Code Playgroud)

0xA*_*xAX 7

方法的最后一个参数可以在前面加一个星号(*),有时也称为"splat"运算符.这表示可以将更多参数传递给函数.收集这些参数并创建数组.

例:

def sample (*test)
    puts test
end
Run Code Online (Sandbox Code Playgroud)

并称之为:

sample(1,2,3)
Run Code Online (Sandbox Code Playgroud)

将返回数组: [1,2,3]

irb(main):001:0> def s(*t)
irb(main):002:1> puts t
irb(main):003:1> end
=> nil
irb(main):004:0> s(1,2,3)
1
2
3
=> nil
Run Code Online (Sandbox Code Playgroud)