Ruby method
The Ruby method is similar to functions in other programming languages. The Ruby method is used to bundle one or more duplicate statements into a unit.
The method name should begin with a lowercase letter. If you start a method name with an uppercase letter, Ruby may treat it as a constant, resulting inincorrect parsing of the call.
The method should be defined before the call, otherwise Ruby will generate an undefined method call exception.
Grammar
defmethod_name[([arg[=default]]...[, *arg[, &expr]])]expr..end
So, you can define a simple method, as follows:
defmethod_nameexpr..end
You can define a method that accepts parameters, as follows:
defmethod_name(var1,var2)expr..end
You can set the default value for the parameter, and use the default value if the necessary parameters are not passed when the method is called:
defmethod_name(var1=value1,var2=value2)expr..end
When you want to call a method, you only need to use the method name, as follows:
method_name
However, when you call a method with parameters, you also need to write the method name with parameters, such as:
method_name25,30
The biggest disadvantage of using a method with parameters is that you need to remember the number of parameters when calling the method. For example, if you pass only two parameters to a method that accepts three parameters, Ruby displays an error.
Example
#!/usr/bin/ruby#-*- coding: UTF-8
-*-deftest(a1="Ruby",a2="Perl")puts"The programming language
is #{a1}"puts"The programming language is
#{a2}"endtest"C","C++"test
The output of the above instance is as follows:
The programming language is C
The programming language is C++
The programming language is Ruby
The programming language is Perl
Return a value from a method
Each method in Ruby returns a value by default. The value returned is the value of the last statement. For example:
Example
deftesti=100j=10k=0end
When this method is called, the last declared variable is returned k
.
Ruby return
statement
In Ruby return
statement is used to return one or more values from the Ruby method.
Grammar
return[expr[\`,'expr...]]
If more than two expressions are given, the array containing these values will be the return value. If no expression is given nil
will be the return value.
Example
return or return12 or return1,2,3
Look at the following example:
Example
#!/usr/bin/ruby#-*- coding: UTF-8
-*-deftesti=100j=200k=300returni,j,kendvar=testputsvar
The output of the above instance is as follows:
100
200
300
Variable number of parameters
Suppose you declare a method with two parameters, and when you call the method, you need to pass two parameters at the same time.
However, Ruby allows you to declare methods with a variable number of parameters. Let’s look at the following example:
Example
#!/usr/bin/ruby#-*- coding: UTF-8 -*-defsample(*test)puts"The number of parameters is
#{test.length}"foriin0...test.lengthputs"The parameter value is
#{test[i]}"endendsample"Zara","6","F"sample"Mac","36","M","MCA"
In this code, you have declared a method sample
to accept a parameter test
. However, this parameter is a variable parameter. This means that parameters can have a different number of variables. The output of the aboveinstance is as follows:
The number of parameters is 3
The parameter value is Zara
The parameter value is 6
The parameter value is F
The number of parameters is 4
Parameter value is Mac
The parameter value is 36
The parameter value is M
The parameter value is MCA
Class method
When a method is defined outside the class, the method is marked by default as private
. On the other hand, if the method is defined in the class, the default tag is public
.
Method default visibility and private
tags can be passed through the module (Module) public
or private
change.
When you want to access the methods of a class, you first need to instantiate the class. Then, using the object, you can access any member of the class.
Ruby provides a way to access methods without instantiation. Let’s look at how to declare and access class methods:
classAccountsdefreading_chargeenddefAccounts.return_dateendend
We already know the way return_date
. How it was declared. It is declared by following the class name with a period followed by the method name. You can access class methods directly, as follows:
Accounts.return_date
To access this method, you do not need to create a class Accounts
the object.
Ruby alias statement
This statement is used to alias a method or global variable. Aliases cannot be defined within the method body. Even if the method is overridden, the alias of the method maintains the current definition of the method.
Is a numbered global variable ( $1,$2,...
) aliases are forbidden. Overriding built-in global variables can cause serious problems.
Grammar
Alias method name method name alias global variable global variable
Example
aliasfoobaralias$MATCH$&
Here, we have been working for bar
defines an alias as foo
for $&
defines an alias as $MATCH
.
Ruby undef
statement
This statement is used to undefine a method. undef
cannot appear in themethod body.
By using the undef
and alias
the interface of the class can be modified independently from the parent class, but be aware that it can breakthe program when calling its own internal methods
undef method name
Example
The following example cancels the name bar
the method definition:
undefbar