Complete the code to define a method that accepts any number of arguments.
def greet([1]) puts "Hello, #{args.join(', ')}!" end
Using *args allows the method to accept any number of arguments as an array.
Complete the code to call the method with multiple arguments.
greet([1])Passing multiple arguments separated by commas matches the *args parameter.
Fix the error in the method definition to correctly accept variable arguments.
def sum_numbers([1]) sum = 0 args.each { |num| sum += num } sum end
The method must use *args to accept variable-length arguments as an array.
Fill both blanks to create a method that sums only positive numbers from variable arguments.
def sum_positive([1]) args.select { |n| n [2] 0 }.sum end
*args collects all arguments, and > filters positive numbers.
Fill all three blanks to define a method that prints each argument with its index.
def print_args([1]) args.each_with_index do |[2], [3]| puts "#{index}: #{arg}" end end
*args collects arguments, and each_with_index provides each argument and its index.