Explain the methods in Ruby and the Ruby Methods

Source: Internet
Author: User

Explain the methods in Ruby and the Ruby Methods

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:

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

Therefore, you can define a simple method as follows:

def method_name   expr..end

This parameter can be used to indicate a method:

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:

def method_name (var1=value1, var2=value2)  expr..end

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

Copy codeThe Code is as follows: method_name

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

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

#!/usr/bin/rubydef test(a1="Ruby", a2="Perl")  puts "The programming language is #{a1}"  puts "The programming language is #{a2}"endtest "C", "C++"test

This produces the following results:

The programming language is CThe programming language is C++The programming language is RubyThe 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:

def test  i = 100  j = 10  k = 0end

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:

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

 Copy codeThe Code is as follows: return

OR

Return 12

OR

Return 1, 2, 3

Let's take a look at this example:

#!/usr/bin/rubydef test  i = 100  j = 200  k = 300return i, j, kendvar = testputs var

This produces the following results:

100200300

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:

#!/usr/bin/rubydef sample (*test)  puts "The number of parameters is #{test.length}"  for i in 0...test.length   puts "The parameters are #{test[i]}"  endendsample "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:

The number of parameters is 3The parameters are ZaraThe parameters are 6The parameters are FThe number of parameters is 4The parameters are MacThe parameters are 36The parameters are MThe 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:

class Accounts  def reading_charge  end  def Accounts.return_date  endend

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:

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:

alias method-name method-namealias global-variable-name global-variable-name

For example:

alias foo baralias $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 codeThe Code is as follows: undef method-name

Instance:

The method for canceling bar definition is as follows:

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.