Params Ruby
#
Different types of parameter in Ruby:#
1. Positional Params#
2. Keyword Param- Introduced at Ruby 2.0.
- Keyword arguments allow us to switch the order of the arguments, without affecting the behavior of the method.
- Also known as named parameters, keyword arguments, required keyword arguments.
#
Passing default value:#
3. Variable Arguments:The **x is the same as variable arguments, but for keyword arguments. It will be a hash instead of an array.
#
4. Catch-all Argument:This means that the method is accepting any arguments, but it’s not doing anything with them. It’s similar to using the underscore character (_) inside a block to show which arguments you aren’t using.
#
Order#
Benefits of Keyword params:- Order doesn't matter
- If order chagges on the method parameter, argument doesn't have to change on all the places where it's called.
- Usually, the code clarity and maintainability gained from keyword arguments outweigh the terseness offered by positional arguments.
#
Parameter vs Arguments:Argument:
- The entity that we can pass to the method during the call is called an argument.
Parameter:
- The entity that we declare when we define a method is called a parameter.
When we pass arguments to a method, the method creates a local variable which has the same name
In ruby, arguments inside a method are passed by reference
#
Interesting Facts:#
1. Parentheses for paramters are optional:But it's recommended for redeability.