My Ruby 💌 newsletter 💌 is available here ! Feel free to subscribe ! 🚀

Method Arguments in Ruby: Part I

In this article we’re going to explore the following topics:

  • methods without arguments
  • methods with arguments
  • argument’s default value
  • variadic arguments

methods without arguments

A method can be defined and invoked without argument

def method_without_arg()
"without arg"
end
def method_without_arg_and_parentheses
"without arg & parentheses"
end
irb> method_without_arg
=> "without arg"
irb> method_without_arg()
=> "without arg"
irb> method_without_arg_and_parentheses
=> "without arg & parentheses"
irb> method_without_arg_and_parentheses()
=> "without arg & parentheses"

We invoke the method_without_arg and method_without_arg_and_parentheses methods with and without parentheses.

By convention, prefer to call method without parentheses. In effect, this syntax “fits” to the mechanism of Message Handling that is implemented by Ruby.

NB: feel free to have a look to this article if you’re unfamiliar with the notion of Message Handling in Ruby.

methods with arguments

On the other hand, a method can also be defined and invoked with arguments

def method_with_args arg1
"with args: #{arg1}"
end
def method_with_args_and_parentheses(arg1)
"with args & parentheses: #{arg1}"
end
irb> method_with_args 'an argument'
=> "with args: an argument"
irb> method_with_args('an argument')
=> "with args: an argument"
irb> method_with_args_and_parentheses 'an argument'
=> "with args & parentheses: an argument"
irb> method_with_args_and_parentheses('an argument')
=> "with args & parentheses: an argument"

We invoke the method_with_args and method_with_args_and_parentheses methods with and without parentheses.

A space between the method name and the first argument is required when the latter one is not surrounded by parentheses.

arguments’ default values

You can supply a default value for an argument. In this case, if a value for the argument isn’t supplied, then the default value will be used instead

def method_with_default_value(newsletter = 'ruby.devscoop.fr')
"The Ruby newsletter is #{newsletter}"
end
irb> method_with_default_value 'awesome'
=> "The Ruby newsletter is awesome"
irb> method_with_default_value
=> "The Ruby newsletter is ruby.devscoop.fr"

If the newsletter argument isn’t supplied at method call then the default value ruby.devscoop.fr is used as value of the newsletter argument.

Otherwise, the supplied value at method call is used.

variadic arguments

Ruby methods can support a variable-length argument lists

def method_with_varargs *names
puts names.class, "\n"
names.each {|name| puts name}
end
irb> method_with_varargs 'Mehdi', 'John', 'Sam'
Array
Mehdi
John
Sam

In the above example, we can see that by prepending an argument name with a * (a.k.a the splat operator), the defined method can accept a variadic number of argument.

All the arguments passed at method call will be stored in a array contained in the argument variable — the names variable in the above example.

Part II

In the part 2 we’ll cover the following topics:

  • keyword arguments
  • block as argument

Voilà !


Thank you for taking the time to read this post :-)

Feel free to 👏 and share this Medium post if it has been useful for you.

Here is a link to my last medium post: Loading a file in Ruby.