Methods In Ruby

Source: Internet
Author: User

Methods In Ruby

This article mainly introduces the concept of methods in Ruby, including basic knowledge about method customization and return values. For more information, see

The Ruby method is very similar to the functions in other programming languages. The Ruby method is used to bind one or more repeated statements in a unit.

The method name should start with a lowercase letter. If the name of a method starts with an uppercase letter, Ruby may regard it as a constant, so it can be correctly parsed and called.

Methods should be defined before Ruby calls them; otherwise, an undefined exception method call will be triggered.

Syntax:

?

1

2

3

Def method_name [([arg [= default]... [, * arg [, & expr])]

Expr ..

End

Therefore, you can define a simple method as follows:

?

1

2

3

Def method_name

Expr ..

End

This parameter can be used to indicate a method:

?

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 for calling the method will be used:

?

1

2

3

Def method_name (var1 = value1, var2 = value2)

Expr ..

End

Whenever you call a method, you only need to write the method name as follows:

The Code is as follows:

Method_name

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

The Code is as follows:

Method_name 25, 30

The most important defect in using methods with parameters is the number of parameters that need to be remembered every time these methods are called. For example, if a method accepts only two parameters, 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 is #{a2 }"

End

Test "C", "C ++"

Test

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

Return the default value for each method in Ruby. The returned value is 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 final declared variable k value will be returned.

Ruby return statement:

The return Statement of Ruby is used to return one or more values from a Ruby method.

Syntax:

The Code is as follows:

Return [expr [', 'expr...]

If more than two expressions are given, an array containing these values will return the value. If there is no expression, it will be the nil value.

Instance:

The Code is as follows:

Return

OR

Return 12

OR

Return 1, 2, 3

Let's 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 produces the following results:

?

1

2

3

100

200

300

Variable target parameters:

Assume that two parameters are required to declare a method. Every time you call this method, two parameters need to be passed along with it.

However, Ruby allows declarations and variable parameters. 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, we have declared that we have accepted a parameter test method example. However, this parameter is a variable parameter. This means that this parameter can be set to 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 as private by default. On the other hand, the owner defined in the class definition is named public by default. You can change the default visibility and private flag methods by public or private modules.

Every time you want to access a class method, you must first instantiate the class. Then, the object can be used to access members of any class.

Ruby provides a method for access without instantiating a class. Let's take a look at how to declare a class method and access:

?

1

2

3

4

5

6

Class Accounts

Def reading_charge

End

Def Accounts. return_date

End

End

Let's look at the return_date declaration method. Declare the next period. This is the second method name and class name. You can directly access this class using the following methods:

?

1

Accounts. return_date

To use this method, you do not need to create accounts such as objects.

Ruby alias statement:

The alias of a method or global variable. Aliases cannot be defined in the method body. The method aliase maintains the currently defined method, even if the method is overwritten.

Aliases for global variables ($1, $2,...) are not allowed. Overwriting the built-in global variables may cause serious problems.

Syntax:

?

1

2

Alias method-name

Alias global-variable-name

For example:

?

1

2

Alias foo bar

Alias $ MATCH $ &

Here we define the foo alias bar and the $ MATCH function alias $ &

Ruby undef statement:

This cancels the method definition. One is that undef cannot appear in the method body.

By using undef and alias, You can independently modify the class interface from the super class, but note that this may be broken by the internal method calls of the program.

Syntax:

Copy the Code as follows:

Undef method-name

Instance:

The method for canceling bar definition is as follows:

?

1

Undef bar

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.