萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 編程語言綜合 >> 詳細解析Ruby中的變量

詳細解析Ruby中的變量

  這篇文章主要介紹了詳細解析Ruby中的變量,是Ruby學習中最基礎的知識之一,需要的朋友可以參考下

  變量持有要使用的程序的數據的存儲位置。

  Ruby支持的有五種類型的變量。在前面的章節中已經經歷了一個簡短描述以及這些變量。本章中介紹的這五種類型的變量。

  Ruby的全局變量:

  全局變量以$開頭。未初始化的全局變量的值是零,並使用-w選項產生警告。

  全局變量的賦值會改變全局狀態。這是不推薦使用全局變量。他們使得程序的含義模糊。

  下面是一個例子顯示使用全局變量。

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 #!/usr/bin/ruby   $global_variable = 10 class Class1 def print_global puts "Global variable in Class1 is #$global_variable" end end class Class2 def print_global puts "Global variable in Class2 is #$global_variable" end end   class1obj = Class1.new class1obj.print_global class2obj = Class2.new class2obj.print_global

  這裡$global_variable是一個全局變量。這將產生以下結果:

  注意: 在Ruby中,把一個哈希號(#)字符,在任意變量或常量之前能夠訪問它的值。

  Global variable in Class1 is 10

  Global variable in Class2 is 10

  Ruby的實例變量:

  實例變量@開始。未初始化的實例變量的值是零,並產生警告-w選項。

  下面是一個例子顯示使用實例變量。

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 #!/usr/bin/ruby   class Customer def initialize(id, name, addr) @cust_id=id @cust_name=name @cust_addr=addr end def display_details() puts "Customer id #@cust_id" puts "Customer name #@cust_name" puts "Customer address #@cust_addr" end end   # Create Objects cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2=Customer.new("2", "Poul", "New Empire road, Khandala")   # Call Methods cust1.display_details() cust2.display_details()

  這裡的@cust_id, @cust_name 和 @cust_addr 都是實例變量。這將產生以下結果:

  ?

1 2 3 4 5 6 Customer id 1 Customer name John Customer address Wisdom Apartments, Ludhiya Customer id 2 Customer name Poul Customer address New Empire road, Khandala

  Ruby的類變量:

  類變量以@@開始,它們可以被用來在方法定義之前必須初始化。

  引用未初始化的類變量產生錯誤。類變量之間共享其中的類變量定義的類或模塊的的後代。

  覆蓋類變量產生警告-w選項。

  下面是一個例子顯示使用類變量:

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #!/usr/bin/ruby   class Customer @@no_of_customers=0 def initialize(id, name, addr) @cust_id=id @cust_name=name @cust_addr=addr end def display_details() puts "Customer id #@cust_id" puts "Customer name #@cust_name" puts "Customer address #@cust_addr" end def total_no_of_customers() @@no_of_customers += 1 puts "Total number of customers: #@@no_of_customers" end end   # Create Objects cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya") cust2=Customer.new("2", "Poul", "New Empire road, Khandala")   # Call Methods cust1.total_no_of_customers() cust2.total_no_of_customers()

  @@no_of_customers 是一類變量。這將產生以下結果:

  ?

1 2 Total number of customers: 1 Total number of customers: 2

  Ruby的局部變量:

  局部變量以小寫字母或_開頭。一個局部變量的范圍的范圍類,模塊,def或做相應的結束或塊的左花括號的緊密括號{}。

  當一個未初始化的局部變量被引用,它被解釋為沒有參數的方法調用。

  分配未初始化的局部變量也作為變量聲明。變量開始存在,直到結束的當前范圍內到達。局部變量的生命周期由Ruby進行解析程序時才能確定。

  另外,在上述的例子中,局部變量 id, name 和他addr.

  Ruby的常量:

  常量以大寫字母開頭。在類或模塊定義的常量可以在該類或模塊訪問,所定義外一個類或模塊可以全局訪問。

  常量不能定義在方法內。引用未初始化的常數會產生一個錯誤。分配已初始化一個常數會產生一個警告。

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 #!/usr/bin/ruby   class Example VAR1 = 100 VAR2 = 200 def show puts "Value of first Constant is #{VAR1}" puts "Value of second Constant is #{VAR2}" end end   # Create Objects object=Example.new() object.show

  這裡VAR1和VAR2是常量。這將產生以下結果:

  ?

1 2 Value of first Constant is 100 Value of second Constant is 200

  Ruby的擬變量:

  他們是特殊的變量,局部變量,但外觀像常數。但不能給這些變量分配到任何值。

  self: 當前方法的接收方對象。

  true: 表示真的值。

  false: 表示假的值。

  nil: 表示未定義(undefined)的值.

  __FILE__: 在當前源文件的名稱.

  

copyright © 萬盛學電腦網 all rights reserved