Photo by Sarah Kilian on Unsplash

Method Arguments in Ruby: Part I

Tech - RubyCademy
RubyCademy
Published in
3 min readMay 11, 2018

--

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 an 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 the method without parentheses. In effect, this syntax “fits” 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 argument newsletter isn’t passed at the method call, then the default value ruby.devscoop.fr is used as the value of the newsletter parameter.

Otherwise, the passed value at the method call is used.

variadic arguments

Ruby methods can support 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 an * (a.k.a the splat operator), the defined method can accept a variadic number of arguments.

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

Part II

In part 2 we’ll cover the following topics:

  • keyword arguments
  • block as argument

Feel free to have a look at the Part II.

Ruby Mastery

We’re currently finalizing our first online course: Ruby Mastery.

Join the list for an exclusive release alert! 🔔

🔗 Ruby Mastery by RubyCademy

Also, you can follow us on x.com as we’re very active on this platform. Indeed, we post elaborate code examples every day.

💚

--

--