国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Ruby > 正文

詳細(xì)解析Ruby中的變量

2020-10-29 19:40:07
字體:
供稿:網(wǎng)友

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

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

全局變量以$開頭。未初始化的全局變量的值是零,并使用-w選項(xiàng)產(chǎn)生警告。

全局變量的賦值會改變?nèi)譅顟B(tài)。這是不推薦使用全局變量。他們使得程序的含義模糊。

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

#!/usr/bin/ruby$global_variable = 10class Class1 def print_global   puts "Global variable in Class1 is #$global_variable" endendclass Class2 def print_global   puts "Global variable in Class2 is #$global_variable" endendclass1obj = Class1.newclass1obj.print_globalclass2obj = Class2.newclass2obj.print_global

這里$global_variable是一個全局變量。這將產(chǎn)生以下結(jié)果:

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

Global variable in Class1 is 10
Global variable in Class2 is 10

Ruby的實(shí)例變量:

實(shí)例變量@開始。未初始化的實(shí)例變量的值是零,并產(chǎn)生警告-w選項(xiàng)。

下面是一個例子顯示使用實(shí)例變量。

#!/usr/bin/rubyclass 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"  endend# Create Objectscust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")cust2=Customer.new("2", "Poul", "New Empire road, Khandala")# Call Methodscust1.display_details()cust2.display_details()

這里的@cust_id, @cust_name 和 @cust_addr 都是實(shí)例變量。這將產(chǎn)生以下結(jié)果:

Customer id 1Customer name JohnCustomer address Wisdom Apartments, LudhiyaCustomer id 2Customer name PoulCustomer address New Empire road, Khandala

Ruby的類變量:

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

引用未初始化的類變量產(chǎn)生錯誤。類變量之間共享其中的類變量定義的類或模塊的的后代。

覆蓋類變量產(chǎn)生警告-w選項(xiàng)。

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

#!/usr/bin/rubyclass 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"  endend# Create Objectscust1=Customer.new("1", "John", "Wisdom Apartments, Ludhiya")cust2=Customer.new("2", "Poul", "New Empire road, Khandala")# Call Methodscust1.total_no_of_customers()cust2.total_no_of_customers()

@@no_of_customers 是一類變量。這將產(chǎn)生以下結(jié)果:

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

Ruby的局部變量:

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

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

分配未初始化的局部變量也作為變量聲明。變量開始存在,直到結(jié)束的當(dāng)前范圍內(nèi)到達(dá)。局部變量的生命周期由Ruby進(jìn)行解析程序時才能確定。

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

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

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

#!/usr/bin/rubyclass Example  VAR1 = 100  VAR2 = 200  def show    puts "Value of first Constant is #{VAR1}"    puts "Value of second Constant is #{VAR2}"  endend# Create Objectsobject=Example.new()object.show

這里VAR1和VAR2是常量。這將產(chǎn)生以下結(jié)果:

Value of first Constant is 100Value 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ù)。一個整數(shù)的范圍可以從 -230 到 230-1 或 -262 to 262-1 在此范圍內(nèi)的整數(shù)是Fixnum類的對象,在此范圍之外的整數(shù)存儲在Bignum的類的對象。

編寫整數(shù)使用可選的前導(dǎo)符號,一個可選的基數(shù)表示(0八進(jìn)制,0x表示十六進(jìn)制或二進(jìn)制0b),其次是一串?dāng)?shù)字在相應(yīng)基數(shù)。下劃線字符被忽略的數(shù)字串。

例如:

123         # Fixnum decimal1_234        # Fixnum decimal with underline-500         # Negative Fixnum0377         # octal0xff         # hexadecimal0b1011        # binary?a          # character code for 'a'?/n         # code for a newline (0x0a)12345678901234567890 # Bignum

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

Ruby支持整數(shù)。他們是數(shù)字但帶小數(shù)。浮點(diǎn)數(shù)是Float類的對象,可以是以下任何一種:

例如:

123.4        # floating point value1.0e6        # scientific notation4E20         # dot not required4e+20        # sign before exponential

字串常值:

Ruby字符串是簡單的8位字節(jié)序列,它們是String類的對象。雙引號字符串可以替代和反斜線符號,但不允許單引號替換和只允許反斜線符號 // 和 /'

例如:

#!/usr/bin/ruby -wputs 'escape using "http://"';puts 'That/'s right';

這將產(chǎn)生以下結(jié)果:

escape using "/"That's right

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

#!/usr/bin/ruby -wputs "Multiplication Value : #{24*60*60}";

這將產(chǎn)生以下結(jié)果:

Multiplication Value : 86400

反斜線符號說明:

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

201551290906687.jpg (574×467)

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

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

#!/usr/bin/rubyary = [ "fred", 10, 3.14, "This is a string", "last element", ]ary.each do |i|  puts iend

這將產(chǎn)生以下結(jié)果:

fred103.14This is a stringlast element

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

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

#!/usr/bin/rubyhsh = colors = { "red" => 0xf00, "green" => 0x0f0, "blue" => 0x00f }hsh.each do |key, value|  print key, " is ", value, "/n"end

這將產(chǎn)生以下結(jié)果:

green is 240red is 3840blue is 15

對于更詳細(xì)的Ruby哈希,經(jīng)過 Ruby哈希.
Ruby的范圍:

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

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

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

#!/usr/bin/ruby(10..15).each do |n|   print n, ' ' end

這將產(chǎn)生以下結(jié)果:

10 11 12 13 14 15

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 寿宁县| 韩城市| 饶阳县| 托克逊县| 肇庆市| 沽源县| 钟山县| 松潘县| 资源县| 康保县| 桑日县| 满城县| 临沧市| 平陆县| 吉林市| 阿城市| 伊金霍洛旗| 海盐县| 盐津县| 屏山县| 漳浦县| 尼勒克县| 贵阳市| 南汇区| 尼玛县| 马公市| 临邑县| 芒康县| 游戏| 苍梧县| 甘德县| 米泉市| 城步| 中山市| 鹤峰县| 开化县| 鄂尔多斯市| 治多县| 工布江达县| 黄冈市| 桂平市|