Ruby 簡明教程 Part 4

1.簡介

2.安裝

3.基本語法

4高級進階

 

 

------繼續

3.基本語法

3.8 Methods 方法

Ruby的方法與其它語言的函數類似。

方法名字小寫開始。 

方法應該在調用前定義,否則會導致異常。

格式

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

舉例:

def method_name 
   expr..
end

或
def method_name (var1, var2)
   expr..
end

可以提供參數缺省值
def method_name (var1 = value1, var2 = value2)
   expr..
end

調用無參方法,直接用方法名:
method_name

調用有參方法:
method_name 25, 30

要注意,記住參數個數。

 

def test(a1 = "Ruby", a2 = "Perl")
   puts "The programming language is #{a1}"
   puts "The programming language is #{a2}"
end
test "C", "C++"
test

結果:

The programming language is C
The programming language is C++
The programming language is Ruby
The programming language is Perl

3.8.1 方法返回值

Ruby 每個方法都默認返回一個值。返回的是最後一個語句的值。

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

上例,返回最後一句的變量k 。

3.8.2  return 語句

Return 用來在方法中哦返回一個或多個值。 

Syntax

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

倆個或以上返回值,數組保存返回值;如果無返回表達式,則返回nil. 

例子:

return

或

return 12

或

return 1,2,3

 demo_return.rb:

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

結果:

zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_return.rb
100
200
300

3.8.3 方法參數個數是變量

Ruby 運行定義變參數個數方法。demo_varPar.rb

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"

這個參數是個變量,可以接受任何變量個數。參數前有*。 這個變參,類似C/C++的指針變量。

zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_varPar.rb
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

3.8.4  類方法

類外面定義的方法,默認爲private. 類內定義的方法,默認爲public.

這些默認的private/public 屬性, 可以被模塊的public/private 屬性修改。

要訪問類的方法,首先要實例化類。然後使用對象,可以訪問類的任何成員。

Ruby提供了不必實例化類就可以訪問類方法。下面具體來看。

class Accounts
   def reading_charge
   end
   def Accounts.return_date
   end
end

return_date 是如何定義的?它在類名後,通過點(.)連接。你可以直接訪問類方法如下:

Accounts.return_date訪問

訪問這種類方法,不必創建類對象。

3.8.5 alias 語句

alias 給方法或全局變量起別名。Aliases  不能在方法主體內定義。方法的別名可以保持方法的當前定義,即便方法被overriden .c

給數字全局變量起別名是禁止的。重寫內置全局變量可能導致嚴重問題。

格式

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

舉例
alias foo bar
alias $MATCH $&

上面命令中, foo 是 bar的別名,  $MATCH 是 $&的別名。

3.8.6   undef 語句

undef  撤消對方法的定義。undef 不能出現在方法主體內。

格式

undef method-name

舉例:取消名爲bar方法,

undef bar

3.9  Block 塊

 Block 概念.

  • 由一堆代碼組成.

  • 塊有名字

  • 代碼在大括號內 ({}).

  • 塊是由同塊名相同的函數來調用。

  • 使用yield 語句來調用快。

格式

block_name {
   statement1
   statement2
   ..........
}

下面講如何用一個簡單的yield語句來調用塊. 另外iayong帶參yield 語句調用塊。

3.9.1  yield 語句

demo_yield.rb

def test
   puts "You are in the method"
   yield
   puts "You are again back to the method"
   yield
end
test {puts "You are in the block"}

執行結果:

zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_yield.rb
You are in the method
You are in the block
You are again back to the method
You are in the block



可以給 yield 傳參。 demo_yieldpar.rb

 

def test
   yield 5
   puts "You are in the method test"
   yield 100
end
test {|i| puts "You are in the block #{i}"}

結果:

zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_yieldpar.rb
You are in the block 5
You are in the method test
You are in the block 100

這兒,yield 後跟着參數。你甚至可以傳多個參數。 在{}塊內,在倆個垂直線內放一個變量來解釋參數。

查看以下語句:

test {|i| puts "You are in the block #{i}"}

 

如果要傳倆個參數,則

yield a, b

對於調用方式:

test {|a, b| statement}

3.9.2 Blocks and Methods 塊和方法

用yield語句,從一個和塊同名的方法中調用塊。

 

def test
   yield
end
test{ puts "Hello world"}

這是最簡單的實現塊的例子。用yield 語句調用test 塊。T

不過如果方法的參數前有&符合,那麼你可以把一個塊傳給這個方法,塊會被賦值給最後的參數。如果參數中* 和 & 符號,&靠後。

 

def test(&block)
   block.call
end
test { puts "Hello World!"}

執行結果:

zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_blockPar.rb
Hello World!

3.9.3 BEGIN and END Blocks

Ruby  代碼文件都可以聲明代碼塊,當文件載入時運行(BEGIN 塊),程序執行完後,再執行END 塊。

 

一個程序可以有多個BEGIN 和END塊。BEGIN塊順序執行,END塊反序執行。

demo_beginend2.rb

 

BEGIN {
   puts "BEGIN code  1  block"
}
END {
   puts "END code 1 block"
}
END {
   puts "End code 2 block"
}
BEGIN {
   puts "Begin code 2 block"
}
   # MAIN block code 
puts "MAIN code block"

執行結果:

zzl@zzl-VirtualBox:~/rubyprojects$ ruby demo_beginend2.rb
BEGIN code  1  block
Begin code 2 block
MAIN code block
End code 2 block
END code 1 block
 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章