Ruby methods and othersprograming languageThe functions in are very similar. Ruby methods are used to bind to one or more repeated statements in a unit.
Method names should start with lowercase letters. If the name of a method starts with an uppercase letter, ruby may think that this is a constant, so the call can be parsed correctly.
Methods should be defined before Ruby calls them, otherwise an exception will be thrown for undefined method calls.
Syntax:
1
2
3
|
def method_name [( [arg [= default]]...[, * arg [, &expr ]])] expr.. end |
Therefore, a simple method can be defined as follows:
1
2
3
|
def method_name expr.. end |
You can express a method and accept such parameters:
1
2
3
|
def method_name (var1, var2) expr.. end |
You can set the default value. If you do not pass the required parameters, the parameters of the calling method will be used to:
1
2
3
|
def method_name (var1=value1, var2=value2) expr.. end |
Whenever it is easy to call a method, just write the name of the method as follows:
However, when calling a method with parameters, write the method name and parameters, such as:
The most important drawback to using methods with parameters is the number of parameters to remember whenever these methods are called. For example, if a method accepts three parameters and only two are passed, Ruby’s will display an error.
example:
1
2
3
4
5
6
7
8
|
#!/usr/bin/ruby def test(a1= "Ruby" , a2= "Perl" ) puts "The programming language is #{a1}" puts "The programming language is #{a2}" end test "C" , "C++" test |
This will produce the following results:
1
2
3
4
|
The programming language is C The programming language is C ++ The programming language is Ruby The programming language is Perl |
Return value from method:
Each method in Ruby returns the default value. This return value will be the value of the last statement. For example:
1
2
3
4
5
|
def test i = 100 j = 10 k = 0 end |
When this method is called, the value of the last declared variable K will be returned.
Ruby return statement:
Ruby’s return statement is used to return one or more values from a ruby method.
Syntax:
If more than two expressions are given, the array containing these values will return values. If there is no expression, the nil value will be returned.
example:
return
OR
return 12
OR
return 1,2,3
Take a look at this example:
1
2
3
4
5
6
7
8
9
10
|
#!/usr/bin/ruby def test i = 100 j = 200 k = 300 return i, j, k end var = test puts var |
This will produce the following results:
1
2
3
|
100 200 300 |
Variable parameters:
Suppose that declaring a method requires two parameters. Whenever you call this method, you need to pass two parameters along with it.
But Ruby allows you to declare methods with variable arguments. Let’s take a look at this example:
1
2
3
4
5
6
7
8
9
10
|
#!/usr/bin/ruby def sample (*test) puts "The number of parameters is #{test.length}" for i in 0 ...test.length puts "The parameters are #{test[i]}" end end sample "Zara" , "6" , "F" sample "Mac" , "36" , "M" , "MCA" |
In this code, you have declared to accept a parameter test method example. However, this parameter is a variable parameter. This means that this parameter can be in any number of variables. Therefore, the above code will produce the following results:
1
2
3
4
5
6
7
8
9
|
The number of parameters is 3 The parameters are Zara The parameters are 6 The parameters are F The number of parameters is 4 The parameters are Mac The parameters are 36 The parameters are M The parameters are MCA |
Class method:
When a method is defined outside the class definition, the method is marked private by default. On the other hand, the party defined in the class definition is public by default. Default visibility and private tagging methods can be changed by public or private modules.
Whenever you want to access a class’s method, you first need to instantiate the class. Objects can then be used to access members of any class.
Ruby provides a method to access the method without instantiating a class. Let’s see how to declare the method and access of a class:
1
2
3
4
5
6
|
class Accounts def reading_charge end def Accounts.return_date end end |
Check the method return_ Date declaration. Declare the subsequent period, which is followed by the method name and class name. The methods to directly access this class are as follows:
1
|
Accounts.return_date |
To use this method, you do not need to create accounts such as objects.
Ruby alias statement:
Alias of method or global variable. Alias cannot be defined in method body. The method defined by alias remains the current method.
Is a global variable ($1, $2,…) Aliasing is prohibited. Overriding built-in global variables can cause serious problems.
Syntax:
1
2
|
alias method-name method-name alias global-variable-name global-variable-name |
For example:
1
2
|
alias foo bar alias $MATCH $& |
Here we define the alias bar of Foo and the alias $& of the $match function
Ruby undef statement:
This cancels the definition of the method. One is that undef cannot appear in the method body.
By using undef and alias, you can modify the class interface independently from the superclass, but note that this may be broken, and the program will call its own method internally.
Syntax:
example:
The method of de defining bar is as follows:
1
|
undef bar |