The concept of methods in Ruby

Source: Internet
Author: User
Tags definition class definition uppercase letter

This article mainly introduces the concept of the method in Ruby, including the method of customization and return value of the basic knowledge, need friends can refer to the following

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:

?

1 2 3 def method_name [[arg [= default]] ... [, * arg [, &expr]]] Expr.. End

So, you can define a simple method as follows:

?

1 2 3 def method_name Expr. End

You can represent a method and accept such an argument:

?

1 2 3 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:

?

1 2 3 def method_name (var1=value1, var2=value2) expr. End

Whenever the method is invoked simply, write the name of the method as follows:

The code is as follows:

Method_name

However, when you call a method with parameters, write the name of the method and the parameters, such as:

The code is as follows:

Method_name 25, 30

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:

?

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 I S #{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 are 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:

?

1 2 3 4 5 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:

The code is 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:

The code is as follows:

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 = j = k = return I, j, K-end var = test puts Var

This will produce the following results:

?

1 2 3 100 200 300

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:

?

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 "," + "," M "," MCA "

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:

?

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 I s 4 The parameters are Mac the parameters are parameters are parameters M the 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:

?

1 2 3 4 5 6 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:

?

1 Accounts.return_date

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.

As global variable ($ 1,$ 2, ...) The alias is prohibited. Overwriting a built-in global variable can cause serious problems.

Grammar:

?

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 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:

Undef Method-name

Instance:

To cancel the definition of the bar method, as follows:

?

1 undef Bar
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.