Latest web development tutorials

Ruby method

Ruby method is similar to other programming language functions. Ruby method for bundling one or more duplicate statements into one unit.

The method name should begin with a lowercase letter. If you are in uppercase letters as the beginning of the method name, Ruby probably will use it as a constant, resulting in incorrectly parse call.

Before calling the method should be defined otherwise, Ruby will produce undefined method call 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 define a method to accept parameters, as follows:

def method_name (var1, var2)
   expr..
end

You can set the default value for a parameter, the parameter is not required to pass the default value if the method call:

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

When you want to call a method, just use the method name, as shown below:

method_name

However, when you call a method with parameters, the parameters that you have to take when writing the method name, for example:

method_name 25, 30

The method of using the parameters biggest drawback is the need to remember the number of parameters when calling the method. For example, if you are the one that accepts three parameters method only passing two arguments, Ruby will display an error.

Examples

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

def test(a1="Ruby", a2="Perl")
   puts "编程语言为 #{a1}"
   puts "编程语言为 #{a2}"
end
test "C", "C++"
test

Run the above example output is:

编程语言为 C
编程语言为 C++
编程语言为 Ruby
编程语言为 Perl

The return value from a method

Ruby each method returns a default value. This value is the value returned by the last statement. E.g:

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

When calling this method will return a final declaration of the variable k.

Rubyreturn statement

Ruby in thereturnstatement is used to return one or more values from a Ruby method.

grammar

return [expr[`,' expr...]]

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

Examples

return

或

return 12

或

return 1,2,3

Consider the following examples:

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

def test
   i = 100
   j = 200
   k = 300
return i, j, k
end
var = test
puts var

Run the above example output is:

100
200
300

Variable number of arguments

Let's say you declare a method that takes two arguments, when you call this method, you also need to pass two parameters.

However, Ruby allows you to declare a variable number of parameters. Let's look at the following examples:

#!/usr/bin/ruby
# -*- coding: UTF-8 -*-

def sample (*test)
   puts "参数个数为 #{test.length}"
   for i in 0...test.length
      puts "参数值为 #{test[i]}"
   end
end
sample "Zara", "6", "F"
sample "Mac", "36", "M", "MCA"

In this code, you have declared a method of sample, accepts a parameter test. However, this parameter is a variable parameter. This implies that the parameters may be provided with a different number of variables. Run the above example output is:

参数个数为 3
参数值为 Zara
参数值为 6
参数值为 F
参数个数为 4
参数值为 Mac
参数值为 36
参数值为 M
参数值为 MCA

Class methods

When the external, the method method defined in class default marked asprivate.On the other hand, if the method defined in the class, the default is marked as public.

The default method of visibility andprivatetag can be changed via the module (Module) inpublicorprivate.

When you want to access the class method, you first need to instantiate the class. Then, using the object, you can access any member of the class.

Ruby provides a way to access without instantiation methods. Let's see how to declare and access the class method:

class Accounts
   def reading_charge
   end
   def Accounts.return_date
   end
end

We already know how the method return_date statement. It is after the class name followed by a dot, followed by the method name after the dot to the declaration. You can directly access the class method as follows:

Accounts.return_date

To access this method, you do not need to create an object class Accounts.

Rubyalias statement

This statement is used as a method or global variables from aliases. Aliases can not be defined within the method body. Even if the method is overridden, the alias method also maintains the current definition of the method.

Number of global variables ($ 1, $ 2, ...) from the alias is prohibited. Rewrite the built-in global variables can cause serious problems.

grammar

alias 方法名 方法名
alias 全局变量 全局变量

Examples

alias foo bar
alias $MATCH $&

Here, we have defined an alias for foo to bar, to define an alias for $ & $ MATCH.

Rubyundef statement

This statement is used to cancel the method definition.undefcan not appear within a method body.

By usingundefandalias,the class interface can be modified independently from the parent class, but please note that when its own internal method calls, it may undermine the program.

grammar

undef 方法名

Examples

The following examples are canceled namedbarmethod definition:

undef bar