Ruby methods are very similar to functions in other programming languages, and Ruby methods are used to bundle one or more duplicate statements in one cell.
The method name should start with a lowercase letter. If the name of a method starts with an uppercase letter, Ruby might think that this is a constant, so the call can be parsed correctly.
Method should define Ruby before calling them, or it will throw a method call that is undefined by an exception.
Grammar:
def method_name [[arg [= default]] ... [, * arg [, &expr]]]
Expr..
End
So, you can define a simple method as follows:
def method_name
Expr.
End
You can represent a method and accept such an argument:
def method_name (var1, var2)
expr.
End
You can set the default value, and the arguments to call the method without passing the required arguments will be used to:
def method_name (var1=value1, var2=value2)
expr.
End
Whenever the method is invoked simply, write the name of the method as follows:
Copy Code code as follows:
However, when you call a method with parameters, write the name of the method and the parameters, such as:
Copy Code code as follows:
The most important flaw in using a method with parameters is the number of parameters that need to be remembered whenever these methods are called. For example, if a method accepts three parameters to pass only two, then Ruby will display an error.
Instance:
#!/usr/bin/ruby
def test (a1= "Ruby", a2= "Perl")
puts "the programming language is #{a1}"
puts " Programming language is #{a2} '
end
test ' C ', ' C + + '
test
This will produce the following results:
The programming language is C the
programming language is C + + The
programming language is Ruby the Programm
ing language is Perl
To return a value from a method:
Each method in Ruby returns a default value. This return value will be the value of the last statement. For example:
def test
i =
J = Ten
k = 0
End
When this method is invoked, the value of the last declared variable K will be returned.
Ruby Return statement:
Ruby's return statement is used for returning one or more values from a Ruby method.
Grammar:
Copy Code code as follows:
return [expr[', ' expr ...]]
If more than two expressions are given, the array contains the values that will return the value. If there is no expression, it will be returned by the nil value.
Instance:
Copy Code code as follows:
Return
OR
Return 12
OR
Return 1,2,3
Take a look at this example:
#!/usr/bin/ruby
def test
i =
j =
k = return
I, J, K-end
var = test
put s Var
This will produce the following results:
Variable number of parameters:
Suppose declaring a method requires two parameters. Every time you call this method, you need to pass two parameters along with it.
But Ruby allows you to declare a method with a variable number of arguments. Let's take a look at this example:
#!/usr/bin/ruby
def sample (*test)
puts "the number of parameters is #{test.length}" to
I in 0...test.length
puts "The parameters are #{test[i]}" End-
Sample "Zara", "6", "F"
Sample "Mac", "a", "M", "MC A
In this code, you have declared acceptance of an example of a parameter test method. However, this parameter is a variable parameter. This means that this parameter can be in any number of variables. So the code above will produce the following results:
The number of parameters is 3 the
parameters are Zara the
parameters are 6 The
parameters are F the
nu Mber of parameters is 4 the parameters are Mac the parameters are parameters
M the
are RS are MCA
Class method:
When a method is defined outside of a class definition, the method is marked as private by default. On the other hand, the party defined in the class definition is the default label public. You can change the default visibility and private tag methods by public or private modules.
Whenever you want to access a method of a class, you first need to instantiate the class. The object can then be used to access members of any class.
Ruby provides a way to access a method that does not instantiate a class. Let's take a look at how to declare a class's methods and access:
Class Accounts
def reading_charge
end
def accounts.return_date
End
Look at the method Return_date declaration. Declaration of a subsequent period, which is followed by the method name and class name. This class can be accessed directly by the following methods:
To use this method, you do not need to create an account such as an object.
Ruby alias statement:
The alias of the method or global variable. Aliases cannot be defined in the method body. Method Aliase maintains the currently defined method, even if the method is overwritten.
is a global variable ($ 1,$ 2, ...). The alias is prohibited. Overwriting a built-in global variable can cause serious problems.
Grammar:
Alias Method-name Method-name
alias Global-variable-name global-variable-name
For example:
Alias Foo Bar
alias $MATCH $&
Here we define Foo's alias bar and $match function alias $&
Ruby undef statement:
This cancels the method definition. One is that undef cannot be present in the method body.
By using undef and alias, you can modify the interface of the class independently from the superclass, but note that this may be invoked by the internal method of the program.
Grammar:
Copy Code code as follows:
Instance:
To cancel the definition of the bar method, as follows: