- 論壇徽章:
- 0
|
總結(jié):Ruby中的@ % # $等各種千奇百怪的符號的含義等
初學(xué)RUBY時(shí),一看各種稍微復(fù)雜一點(diǎn)的代碼時(shí)很容易被RUBY各種約定的表示方法搞暈,這整理一下 。
(若標(biāo)識符首位是小寫字母或“_”,則該標(biāo)識符就是局部變量或方法調(diào)用。)
(以大寫字母([A-Z])開始的標(biāo)識符是常數(shù)、類名或者模塊名)
以@開始的變量是實(shí)例變量,它屬于特定的對象。可以在類或子類的方法中引用實(shí)例變量。
若引用尚未被初始化的實(shí)例變量的話,其值為nil。Ruby的實(shí)例變量無須聲明,每個(gè)實(shí)例變量都是在第一次出現(xiàn)時(shí)動態(tài)加入對象。Ruby的實(shí)例變量通常在方法中定義類聲明——當(dāng)在方法里聲明實(shí)例變量時(shí),該實(shí)例變量實(shí)際上屬于該方法所在類的實(shí)例,而不是屬于該方法。例如- ?1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17 class Apple
- # 定義第一個(gè)方法
- def info1
- # 輸出實(shí)例變量@a
- puts @a
- end
- # 定義第二個(gè)方法
- def info2
- # 為實(shí)例變量賦值
- @a = "Hello";
- end
- end
- # 創(chuàng)建Apple類實(shí)例
- apple = Apple.new
- # 調(diào)用方法
- apple.info2
- apple.info1
復(fù)制代碼 以@@開始的變量是類變量。在類的定義中定義類變量,可以在類的特殊方法、實(shí)例方法等處對類變量進(jìn)行引用/賦值:- ?1
- 2
- 3
- 4
- 5
- 6 class Foo
- @@foo = 1
- def bar
- puts @@foo
- end
- end
復(fù)制代碼 類變量與常數(shù)的區(qū)別如下。
可以重復(fù)賦值(常數(shù)則會發(fā)出警告)
不能在類的外部直接引用(在繼承類中則可以引用/賦值)
類變量與類的實(shí)例變量的區(qū)別如下。
可在子類中引用/賦值
可在實(shí)例方法中引用/賦值
可以把類變量看作一種被類、子類以及它們的實(shí)例所共享的全局變量。- ?class Foo
- @@foo = 1
- end
- class Bar < Foo
- p @@foo += 1 # => 2
- end
- class Baz < Bar
- p @@foo += 1 # => 3
- end
復(fù)制代碼 模塊中定義的類變量(模塊變量)被所有包含該模塊的類所共享。- ?module Foo
- @@foo = 1
- end
- class Bar
- include Foo
- p @@foo += 1 # => 2
- end
- class Baz
- include Foo
- p @@foo += 1 # => 3
- end
-
復(fù)制代碼 以$開始的變量是全局變量,可以在程序的任何地方加以引用(因此需要特別留意)。全局變量無需變量聲明。引用尚未初始化的全局變量時(shí),其值為 nil。
%表示法(百分號表示法):
%{String} 用于創(chuàng)建一個(gè)使用雙引號括起來的字符串
%Q{String} 用于創(chuàng)建一個(gè)使用雙引號括起來的字符串
%q{String} 用于創(chuàng)建一個(gè)使用單引號括起來的字符串
%r{String} 用于創(chuàng)建一個(gè)正則表達(dá)式字面值
%w{String} 用于將一個(gè)字符串以空白字符切分成一個(gè)字符串?dāng)?shù)組,進(jìn)行較少替換
%W{String} 用于將一個(gè)字符串以空白字符切分成一個(gè)字符串?dāng)?shù)組,進(jìn)行較多替換
%s{String} 用于生成一個(gè)符號對象
%x{String} 用于執(zhí)行String所代表的命令
"%05d" % 123 » "00123"
"%-5s: %08x" % [ "ID", self.id ] » "ID:200e1670"
# 用來調(diào)用一個(gè)方法,
*號:
若左邊最后一個(gè)表達(dá)式前帶*的話,則將右邊多余的元素以數(shù)組的形式代入這個(gè)帶*的表達(dá)式中。若右邊沒有多余元素的話,就把空數(shù)組代入其中。- ?*foo = 1, 2, 3 # foo = [1, 2, 3]
- foo,*bar = 1, 2, 3 # foo = 1; bar = [2, 3]
復(fù)制代碼 *用在方法定義中表示可變長的變量:
【FROM “ProgrammingRuby”】- Variable-Length Argument Lists
- But what if you want to pass in a variable number of arguments, or want to capture multiple arguments into a single parameter? Placing an asterisk before the name of the parameter after the ``normal'' parameters does just that.
- ?def varargs(arg1, *rest)
- "Got #{arg1} and #{rest.join(', ')}"
- end
- varargs("one") » "Got one and "
- varargs("one", "two") » "Got one and two"
- varargs "one", "two", "three" » "Got one and two, three"
-
- ?irb(main):005:0> def call_back(*a)
- irb(main):006:1> a.each do |arg|
- irb(main):007:2* puts arg
- irb(main):008:2> end
- irb(main):009:1> end
- => nil
- irb(main):011:0> call_back(["hello",99])
- hello
- 99
- => [["hello", 99]]
- irb(main):012:0> call_back(["hello",99],"hello",99)
- hello
- 99
- hello
- 99
- => [["hello", 99], "hello", 99]
復(fù)制代碼 其他用法:- C:\Users\Administrator>irb
- irb(main):001:0> [1,2,3] * "hi"
- => "1hi2hi3"
- irb(main):002:0> [1,2,3] * ";"
- => "1;2;3"
- irb(main):003:0> [1,2,3] * 3
- => [1, 2, 3, 1, 2, 3, 1, 2, 3]
- irb(main):004:0> "hi" * 3
- => "hihihi"
復(fù)制代碼 |
|