Positional parameters in Ruby
In this article, we will consider the features of working with the positional parameters.
This article is divided into the following sections:
- What is a positional parameter?
- The presence of arguments for the positional parameter is required
- Conclusions
What is a positional parameter?
Positional parameters are the simplest parameters in ruby, as their behavior is very predictable. The positional parameter makes it possible to transfer any object to a method and use it within the method.
Let’s see declaring syntax:
def method(positional_parameter_one, positional_parameter_two)
positional_parameter_one + positional_parameter_two
endmethod(1, 2)
produces:
=> 3The presence of arguments for the positional parameter is required
When calling a method that has positional parameters, the presence of arguments for these parameters is required. If we don’t pass any arguments, then ruby will not know where to get values for local variables inside a method and will tell us about the error.
Get Alexey Sologub’s stories in your inbox
Join Medium for free to get updates from this writer.
For instance:
def method(positional_parameter_one, positional_parameter_two)
positional_parameter_one + positional_parameter_two
endAs you can see, we have 2 positional parameters. Now, if we call a method with only one argument, we’ll see the error:
method(1)produces:
wrong number of arguments (given 1, expected 2)
(repl):2:in `method'
(repl):6:in `<main>'Conclusions
- positional parameters are the most predictable parameters in ruby
- the presence of arguments for the positional parameter is required
Original article on my blog | Follow Me on Twitter | Subscribe to my newsletter



