Menu Close

Articles

Ruby: Methods

A method is a named block of code that can be called multiple times. Other languages have functions, procedures or routines, but in Ruby has methods.  In other words a method is a bunch of statements or expressions.

Create a method

Ruby has weird stuff going on with methods that I will explain one by one later. First let’s see the simple stuff that is comprehensible and comfortable to learn.

  1. First of all a method is always defined with keyword: “def“. There is no “method” keyword,
  2. Second, a method has a lowercase name. Don’t start wit capital letters ever,
  3. A method can be called by just mention its name as a statement,
  4. A method can have parameters bu this is optional,
  5. A method is a block of code so it must be finalized with “end”.

Example:

Notes:

  • In this example the method name is “method” but this is not a keyword is the method name,
  • Variable $variable is visible inside the method since is a global variable,
  • This method do not have parameters and do not have a result.

Method names

I have mention that method names must start with a lowercase letter? It is possible to start with underscore.  In ruby, starting something with underscore means it is not used yet. If you design a function that is not used you start with underscore. In Python if you start a variable with underscore, that variable is private.

Some predefined Ruby methods can have suffix “?”. This kind of function is a “query” method. It is usually getting something as a result. For example “include?” is such a method. 

Some predefined Ruby methods can have suffix “!”. That means the method is dangerous. For example a method that mutate an object could use “!” suffix. 

Some methods that you define are using “=” suffix can be used as “attribute writers”. These are known as “setter methods” in other languages. They are used to make an attribute into a class writable.

If you start a method with uppercase, you may get away with it, but the program will think that it is a constant. So you can’t execute it properly if it has no parameters. If you use () after the name, it will work. 

Homework: Open the example live and run it: simple method

Method parameters

A method can have parameters, defined in a classic way, the same as in Python. Parameter names are using lowercase letters and can have initial value. You must enclose all parameters in round brackets and separate them using comma: (_ , _ , _ , …)

Optional parameters:

Notes:

  • parameter “c” is optional. It has initial value = zero,
  • arguments can pair-up with parameters using names and “=”,
  • order of arguments do not have to be same as order of parameters,
  • a method can return value of expression using “return” statement.

Homework: open this example and run it live: ruby parameters

Variable arguments

Ruby like Python is using the same notation for variable number of arguments. This is a technique to enable capturing multiple optional argument values into a single parameter that is a collection of values.

Var args:

Notes:

  • args in a name we have invented for collection parameter,
  • the parameter *args is using prefix “*” to indicate is a “rest” of arguments collector,
  • in the example, we use (2.0 + args.length) cause result to have decimals.

Splat operator

Similar to role of “*” in front of a parameter, you can use same operator to transform one array into its elements, one by one. You can use this technique to pass parameters to a method that require variable arguments, or to any method.

Example:

Notes:

  1. in this example we introduce new operator “<<” called sometimes “shovel”,
  2. the “array” variable is an Array that you build step by step until you enter “0”,
  3. the splat operator “*” is used in the last statement

Homework: run this example live two times: splat operator

  • first time use number 4, 5, 6, 0
  • second time use numbers 1, 2, 7 , 0

Next article: classes is cumming soon.