Latest web development tutorials

Ruby 變量

變量是持有可被任何程序使用的任何數據的存儲位置。

Ruby 支持五種類型的變量。

  • 一般小寫字母、下劃線開頭:變量(Variable)。
  • $開頭:全局變量(Global variable)。
  • @開頭:實例變量(Instance variable)。
  • @@開頭:類變量(Class variable)類變量被共享在整個繼承鏈中
  • 大寫字母開頭:常數(Constant)。

您已經在前面的章節中大概了解了這些變量,本章節將為您詳細講解這五種類型的變量。

Ruby 全局變量

全局變量以$ 開頭。 未初始化的全局變量的值為nil ,在使用-w選項後,會產生警告。

給全局變量賦值會改變全局狀態,所以不建議使用全局變量。

下面的實例顯示了全局變量的用法。

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

$global_variable = 10
class Class1
  def print_global
      puts "全局变量在 Class1 中输出为 #$global_variable"
  end
end
class Class2
  def print_global
      puts "全局变量在 Class2 中输出为 #$global_variable"
  end
end

class1obj = Class1.new
class1obj.print_global
class2obj = Class2.new
class2obj.print_global

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

注意:在Ruby中,您可以通過在變量或常量前面放置#字符,來訪問任何變量或常量的值。

全局变量在 Class1 中输出为 10
全局变量在 Class2 中输出为 10

Ruby 實例變量

實例變量以@ 開頭。 未初始化的實例變量的值為nil ,在使用-w選項後,會產生警告。

下面的實例顯示了實例變量的用法。

#!/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

# 創建對象cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")

# 調用方法cust1.display_details()
cust2.display_details()

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

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 選項後,重載類變量會產生警告。

下面的實例顯示了類變量的用法。

#!/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

# 创建对象
cust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")
cust2=Customer.new("2", "Poul", "New Empire road, Khandala")

# 调用方法
cust1.total_no_of_customers()
cust2.total_no_of_customers()

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

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

Ruby 局部變量

局部變量以小寫字母或下劃線_ 開頭。 局部變量的作用域從class、module、def 或do 到相對應的結尾或者從左大括號到右大括號{}。

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

對未初始化的局部變量賦值也可以當作是變量聲明。 變量會一直存在,直到當前域結束為止。 局部變量的生命週期在Ruby 解析程序時確定。

在上面的實例中,局部變量是id、name 和addr。

Ruby 常量

常量以大寫字母開頭。 定義在類或模塊內的常量可以從類或模塊的內部訪問,定義在類或模塊外的常量可以被全局訪問。

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

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

class Example
   VAR1 = 100
   VAR2 = 200
   def show
       puts "第一個常量的值為#{VAR1}"
       puts "第二個常量的值為#{VAR2}"
   end
end

# 創建對象object=Example.new()
object.show

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

第一個常量的值為100
第二個常量的值為200

Ruby 偽變量

它們是特殊的變量,有著局部變量的外觀,但行為卻像常量。 您不能給這些變量賦任何值。

  • self:當前方法的接收器對象。
  • true:代表true的值。
  • false:代表false的值。
  • nil:代表undefined的值。
  • __FILE__:當前源文件的名稱。
  • __LINE__:當前行在源文件中的編號。