亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

詳細解析Ruby中的變量

 更新時間:2015年05月12日 09:11:00   投稿:goldensun  
這篇文章主要介紹了詳細解析Ruby中的變量,是Ruby學習中最基礎的知識之一,需要的朋友可以參考下

 變量持有要使用的程序的數(shù)據(jù)的存儲位置。

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

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

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

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

#!/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選項。

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

#!/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 都是實例變量。這將產生以下結果:

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

# 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 是一類變量。這將產生以下結果:

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

Ruby的局部變量:

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

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

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

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

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

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

#!/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是常量。這將產生以下結果:

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

Ruby的擬變量:

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

  •     self: 當前方法的接收方對象。
  •     true: 表示真的值。
  •     false: 表示假的值。
  •     nil: 表示未定義(undefined)的值.
  •     __FILE__: 在當前源文件的名稱.
  •     __LINE__: 在源文件中的當前行號。

Ruby的基本常值:

Ruby使用字面值的規(guī)則是簡單和直觀。本節(jié)介紹了所有基本的Ruby的常值。
整型數(shù):

Ruby支持整數(shù)。一個整數(shù)的范圍可以從 -230 到 230-1 或 -262 to 262-1 在此范圍內的整數(shù)是Fixnum類的對象,在此范圍之外的整數(shù)存儲在Bignum的類的對象。

編寫整數(shù)使用可選的前導符號,一個可選的基數(shù)表示(0八進制,0x表示十六進制或二進制0b),其次是一串數(shù)字在相應基數(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

注:類和對象解釋在本教程中另一個章節(jié)。
浮點數(shù):

Ruby支持整數(shù)。他們是數(shù)字但帶小數(shù)。浮點數(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';

這將產生以下結果:

escape using "\"
That's right

也可以替換成一個字符串使用#{expr}序列表示任何Ruby表達式的值。表達式expr 可以是任何Ruby的表達式。

#!/usr/bin/ruby -w

puts "Multiplication Value : #{24*60*60}";

這將產生以下結果:

Multiplication Value : 86400

反斜線符號說明:

以下是Ruby支持的反斜線符號列表:

201551290906687.jpg (574×467)

 Ruby字符串的更多詳細信息,請通過 Ruby字符串.
Ruby數(shù)組:

Ruby的數(shù)組是由放置對象引用方括號之間用逗號分隔的一系列字面。逗號結尾被忽略。
例如:

#!/usr/bin/ruby

ary = [ "fred", 10, 3.14, "This is a string", "last element", ]
ary.each do |i|
  puts i
end

這將產生以下結果:

fred
10
3.14
This is a string
last element

Ruby的數(shù)組的更多細節(jié),經過 Ruby數(shù)組.
Ruby 哈希:

字面上Ruby創(chuàng)建哈希放置括號之間的鍵/值對列表,以逗號或序列=>之間的鍵和值。逗號結尾被忽略。
例如:

#!/usr/bin/ruby

hsh = colors = { "red" => 0xf00, "green" => 0x0f0, "blue" => 0x00f }
hsh.each do |key, value|
  print key, " is ", value, "\n"
end

這將產生以下結果:

green is 240
red is 3840
blue is 15

對于更詳細的Ruby哈希,經過 Ruby哈希.
Ruby的范圍:

范圍代表的間隔。一組的開始和結束的值??赡鼙皇褂胹..e 和s...e文字,或具有Range.new范圍。

范圍使用..包含運行從開始到結束。創(chuàng)建使用...排除最終值。當作為一個迭代器,范圍序列中的每個值將返回。

range (1..5) 表示,它包括1,2,3,4,5值,range (1...5) 表示,它包括1,2,3,4這四個值。
實例:

#!/usr/bin/ruby

(10..15).each do |n| 
  print n, ' ' 
end

這將產生以下結果:

10 11 12 13 14 15

相關文章

最新評論