Rubi Methods

Ruby methods are very similar to “Functions” in JavaScript, and basically are used to bundle chinks of code in a single unit, methods names should begin with a lowercase letter, and should be defined before calling them, you can pass parameters to methods, and get information that the method return, form more information about methods you can follow the Ruby-Ironman linkĀ 


Defining a Method

For me, methods are like factories, where things are made, but more important the method are like the building, and the code inside is like the machinery, the building need a port of entry where the raw materials are received and and also a port where the final product is send back.

The Method port of entry is where the parameters are received, in the following example a method is receiving two parameters then, we add those two parameters. and return them.

Default Parameters

This meas that you can set up a default value to the parameter incase that you do not get any, but if you receive parameters those will overwrite the defaults.

When returning more than a value they will be returned as an array.

Variable: “Number of Parameters”

If you wan to pass an undefined number of parameters you can pass them as an array using a special variable call: “Number of Parameters” and this is how it works.

 



Copyright 2017. All rights reserved.

Posted January 7, 2017 by Edmundo in category "Ruby