可选参数(splat 运算符)

def welcome_guests(*guests)
    guests.each { |guest| puts "Welcome #{guest}!" }
end

welcome_guests('Tom')    # Welcome Tom!
welcome_guests('Rob', 'Sally', 'Lucas') # Welcome Rob!
                                        # Welcome Sally!
                                        # Welcome Lucas!

请注意,welcome_guests(['Rob', 'Sally', 'Lucas']) 将输出 Welcome ["Rob", "Sally", "Lucas"]!
相反,如果你有一个列表,你可以做 welcome_guests(*['Rob', 'Sally', 'Lucas']),这将作为 welcome_guests('Rob', 'Sally', 'Lucas')