可選引數(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')