變量持有要使用的程序的數(shù)據(jù)的存儲(chǔ)位置。
Ruby支持的有五種類型的變量。在前面的章節(jié)中已經(jīng)經(jīng)歷了一個(gè)簡短描述以及這些變量。本章中介紹的這五種類型的變量。
Ruby的全局變量:
全局變量以$開頭。未初始化的全局變量的值是零,并使用-w選項(xiàng)產(chǎn)生警告。
全局變量的賦值會(huì)改變?nèi)譅顟B(tài)。這是不推薦使用全局變量。他們使得程序的含義模糊。
下面是一個(gè)例子顯示使用全局變量。
#!/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是一個(gè)全局變量。這將產(chǎn)生以下結(jié)果:
注意: 在Ruby中,把一個(gè)哈希號(#)字符,在任意變量或常量之前能夠訪問它的值。
Global variable in Class1 is 10
Global variable in Class2 is 10
Ruby的實(shí)例變量:
實(shí)例變量@開始。未初始化的實(shí)例變量的值是零,并產(chǎn)生警告-w選項(xiàng)。
下面是一個(gè)例子顯示使用實(shí)例變量。
#!/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 都是實(shí)例變量。這將產(chǎn)生以下結(jié)果:
Customer id 1
Customer name John
Customer address Wisdom Apartments, Ludhiya
Customer id 2
Customer name Poul
Customer address New Empire road, Khandala
Ruby的類變量:
類變量以@@開始,它們可以被用來在方法定義之前必須初始化。
引用未初始化的類變量產(chǎn)生錯(cuò)誤。類變量之間共享其中的類變量定義的類或模塊的的后代。
覆蓋類變量產(chǎn)生警告-w選項(xiàng)。
下面是一個(gè)例子顯示使用類變量:
#!/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 是一類變量。這將產(chǎn)生以下結(jié)果:
Total number of customers: 1
Total number of customers: 2
Ruby的局部變量:
局部變量以小寫字母或_開頭。一個(gè)局部變量的范圍的范圍類,模塊,def或做相應(yīng)的結(jié)束或塊的左花括號的緊密括號{}。
當(dāng)一個(gè)未初始化的局部變量被引用,它被解釋為沒有參數(shù)的方法調(diào)用。
分配未初始化的局部變量也作為變量聲明。變量開始存在,直到結(jié)束的當(dāng)前范圍內(nèi)到達(dá)。局部變量的生命周期由Ruby進(jìn)行解析程序時(shí)才能確定。
另外,在上述的例子中,局部變量 id, name 和他addr.
Ruby的常量:
常量以大寫字母開頭。在類或模塊定義的常量可以在該類或模塊訪問,所定義外一個(gè)類或模塊可以全局訪問。
常量不能定義在方法內(nèi)。引用未初始化的常數(shù)會(huì)產(chǎn)生一個(gè)錯(cuò)誤。分配已初始化一個(gè)常數(shù)會(huì)產(chǎn)生一個(gè)警告。
#!/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是常量。這將產(chǎn)生以下結(jié)果:
Value of first Constant is 100
Value of second Constant is 200
Ruby的擬變量:
他們是特殊的變量,局部變量,但外觀像常數(shù)。但不能給這些變量分配到任何值。
- self: 當(dāng)前方法的接收方對象。
- true: 表示真的值。
- false: 表示假的值。
- nil: 表示未定義(undefined)的值.
- __FILE__: 在當(dāng)前源文件的名稱.
- __LINE__: 在源文件中的當(dāng)前行號。
Ruby的基本常值:
Ruby使用字面值的規(guī)則是簡單和直觀。本節(jié)介紹了所有基本的Ruby的常值。
整型數(shù):
Ruby支持整數(shù)。一個(gè)整數(shù)的范圍可以從 -230 到 230-1 或 -262 to 262-1 在此范圍內(nèi)的整數(shù)是Fixnum類的對象,在此范圍之外的整數(shù)存儲(chǔ)在Bignum的類的對象。
編寫整數(shù)使用可選的前導(dǎo)符號,一個(gè)可選的基數(shù)表示(0八進(jìn)制,0x表示十六進(jìn)制或二進(jìn)制0b),其次是一串?dāng)?shù)字在相應(yīng)基數(shù)。下劃線字符被忽略的數(shù)字串。
例如:
123 # Fixnum decimal
1_234 # Fixnum decimal with underline
-500 # Negative Fixnum
0377 # octal
0xff # hexadecimal
0b1011 # binary
?a # character code for 'a'
?\n # code for a newline (0x0a)
12345678901234567890 # Bignum
注:類和對象解釋在本教程中另一個(gè)章節(jié)。
浮點(diǎn)數(shù):
Ruby支持整數(shù)。他們是數(shù)字但帶小數(shù)。浮點(diǎn)數(shù)是Float類的對象,可以是以下任何一種:
例如:
123.4 # floating point value
1.0e6 # scientific notation
4E20 # dot not required
4e+20 # sign before exponential
字串常值:
Ruby字符串是簡單的8位字節(jié)序列,它們是String類的對象。雙引號字符串可以替代和反斜線符號,但不允許單引號替換和只允許反斜線符號 \\ 和 \'
例如:
#!/usr/bin/ruby -w
puts 'escape using "\\"';
puts 'That's right';
這將產(chǎn)生以下結(jié)果:
escape using "\"
That's right
也可以替換成一個(gè)字符串使用#{expr}序列表示任何Ruby表達(dá)式的值。表達(dá)式expr 可以是任何Ruby的表達(dá)式。
#!/usr/bin/ruby -w
puts "Multiplication Value : #{24*60*60}";
這將產(chǎn)生以下結(jié)果:
Multiplication Value : 86400
反斜線符號說明:
以下是Ruby支持的反斜線符號列表:
![](http://img.jbzj.com/file_images/article/201505/201551290906687.jpg?20154129928)
Ruby字符串的更多詳細(xì)信息,請通過 Ruby字符串.
Ruby數(shù)組:
Ruby的數(shù)組是由放置對象引用方括號之間用逗號分隔的一系列字面。逗號結(jié)尾被忽略。
例如:
#!/usr/bin/ruby
ary = [ "fred", 10, 3.14, "This is a string", "last element", ]
ary.each do |i|
puts i
end
這將產(chǎn)生以下結(jié)果:
fred
10
3.14
This is a string
last element
Ruby的數(shù)組的更多細(xì)節(jié),經(jīng)過 Ruby數(shù)組.
Ruby 哈希:
字面上Ruby創(chuàng)建哈希放置括號之間的鍵/值對列表,以逗號或序列=>之間的鍵和值。逗號結(jié)尾被忽略。
例如:
#!/usr/bin/ruby
hsh = colors = { "red" => 0xf00, "green" => 0x0f0, "blue" => 0x00f }
hsh.each do |key, value|
print key, " is ", value, "\n"
end
這將產(chǎn)生以下結(jié)果:
green is 240
red is 3840
blue is 15
對于更詳細(xì)的Ruby哈希,經(jīng)過 Ruby哈希.
Ruby的范圍:
范圍代表的間隔。一組的開始和結(jié)束的值??赡鼙皇褂胹..e 和s...e文字,或具有Range.new范圍。
范圍使用..包含運(yùn)行從開始到結(jié)束。創(chuàng)建使用...排除最終值。當(dāng)作為一個(gè)迭代器,范圍序列中的每個(gè)值將返回。
range (1..5) 表示,它包括1,2,3,4,5值,range (1...5) 表示,它包括1,2,3,4這四個(gè)值。
實(shí)例:
#!/usr/bin/ruby
(10..15).each do |n|
print n, ' '
end
這將產(chǎn)生以下結(jié)果:
您可能感興趣的文章:- Ruby基本的環(huán)境變量設(shè)置以及常用解釋器命令介紹
- Ruby中變量引用時(shí)的一些注意點(diǎn)
- Ruby中類變量和實(shí)例變量的比較
- Ruby 中$開頭的全局變量、內(nèi)部變量、隱藏變量介紹
- ruby 局部變量
- ruby 實(shí)變量
- 深入分析Ruby 變量