Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A method with an optional parameter

Tags:

methods

ruby

Is there a way to make a method that can accept a parameter, but can also be called without one, in which case the parameter is regarded nil like the following?

some_func(variable)  some_func 
like image 575
TamRock Avatar asked Mar 02 '16 12:03

TamRock


People also ask

How do you specify an optional parameter?

Optional parameters are defined at the end of the parameter list, after any required parameters. If the caller provides an argument for any one of a succession of optional parameters, it must provide arguments for all preceding optional parameters. Comma-separated gaps in the argument list aren't supported.

What are the optional named parameters?

Unlike positional parameters, the parameters' name must be specified while the value is being passed. Curly brace {} can be used to specify optional named parameters.


1 Answers

def some_func(variable = nil)   ... end 
like image 56
sawa Avatar answered Oct 13 '22 04:10

sawa