In Ruby, function arguments give a good flexibility on how they can be used and in this short article I will show you how you can use them.

Defining and usage of Function Arguments

To define function arguments in Ruby, you simply list them inside the parentheses(or without) after the method name. Here's an example of a method that takes two arguments:

      
    def greeting(name, age)
      puts "Hello, #{name}! You are #{age} years old."
    end

    #Parentheses are not obligatory:

    def greeting name, age
      puts "Hello, #{name}! You are #{age} years old."
    end
      
    

Default Values for Function Arguments

In Ruby, you can specify default values for function arguments. If an argument is not passed to the method, the default value will be used instead. Here's an example of a method that takes an optional argument with a default value:

      
    def greeting(name, age = 30)
      puts "Hello, #{name}! You are #{age} years old."
    end

    greeting("Alice") # Output: "Hello, Alice! You are 30 years old."
    greeting("Bob", 25) # Output: "Hello, Bob! You are 25 years old."
      
     

Using Named Arguments in Ruby

In Ruby 2.0 and later versions, you can use named arguments to make your code more readable and maintainable. Named arguments allow you to specify arguments by name instead of position. Here's an example:

    
    def greeting(name:, age: 30)
      puts "Hello, #{name}! You are #{age} years old."
    end

    greeting(name: "Alice") # Output: "Hello, Alice! You are 30 years old."
    greeting(name: "Bob", age: 25) # Output: "Hello, Bob! You are 25 years old."
    
  

Using method reference as an function argument

Ruby enables using a method reference as method argument that can be used later(by using .call)

    
    def execute_method method,text
      method.call text
    end

    def display_text(txt)
      puts txt
    end

    method_obj  = self.method("display_text")
    execute_method(method_obj,"Hello from the start method!")
    
  

In this example, we define a method called 'execute_method' that takes two named arguments: method and text. And 'Method' argument will be the one to point to display_text method so it can be called dynamically.

Using Block as an method argument

Ruby enables using blocks as method arguments similar to method references(from above) but with a different syntax.

    
    def use_block a = "Hey"
      yield a
      yield a
    end

    use_block{|s| puts 'where is the money? #{s} '}
    
    

In this example, define block while we call 'use_block' method. And we call it twice by using special keyword 'yield'.

Use Variable Arguments to Capture as Many Values as Necessary

Ruby enables using dynamic amount of arguments for all it's methods.

    
    def print_all(*args)
    end
    print_all(1, 2, 3, 'sdf', 5,5)
    
    

Use Hash Arguments to Capture as Many Values as Necessary

Ruby enables using hash as a dynamic list of arguments

    
    def foo(regular, hash={})
      puts "regular: #{regular}"
      puts "hash: #{hash}"
      puts "a: #{hash[:a]}"
      puts "b: #{hash[:b]}"
    end
       foo("regular argument", a: 12, :b => 13)
      
    

Double splat operator(**) for arguments

The double splat operator came out back in Ruby 2.0. It’s pretty similar to the original splat with one difference: it can be used for hashes! So you can random amount of key value arguments and ruby will read it as an argument

    
        def foo(a, *b, **c)
            [a, b, c]
        end

      foo(10, 20, 30, d: 40, e: 50) will result to: [10, [20, 30], {:d=>40, :e=>50}]
       
    

In this example we define a method 'foo' it will get all 3 types of arguments: simple, splash for arrays and double splash for hashes.

Conclusion

In this article, we've explored how to use function arguments in Ruby. Cheers!