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

methods without arguments

methods with arguments

argument’s default value

variadic arguments

Before to start

I’m thrilled to share with you our latest project: Fun Facts about Ruby — Volume 1

Please feel free to spread the word and share this post! 🙏

Thank you for your time!

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, "

"

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

Feel free to have a look to the Part II .

Voilà !

May I have your attention please 🎤🎤

Feel free to subscribe here: www.rubycademy.com