%{String} 用于創(chuàng)建一個(gè)使用雙引號(hào)括起來的字符串
%Q{String} 用于創(chuàng)建一個(gè)使用雙引號(hào)括起來的字符串
復(fù)制代碼 代碼如下:
str=END_OF_STRING
a string
END_OF_STRING
%Q!Some String of “Characters”! ==> ” Some String of /”Characters/” “
%q{String} 用于創(chuàng)建一個(gè)使用單引號(hào)括起來的字符串
%q!Some String of “Characters”! ==> ‘Some String of Characters'
%r{String} 用于創(chuàng)建一個(gè)正則表達(dá)式字面值
%r{/usr/bin/} ==> ///usr//bin///
%w{String} 用于將一個(gè)字符串以空白字符切分成一個(gè)字符串?dāng)?shù)組,進(jìn)行較少替換
%W{String} 用于將一個(gè)字符串以空白字符切分成一個(gè)字符串?dāng)?shù)組,進(jìn)行較多替換
%W(North South East West) ==> ["North", "South", "East", "West"]
%s{String} 用于生成一個(gè)符號(hào)對(duì)象
%x{String} 用于執(zhí)行String所代表的命令
%x{ ls /usr/local } ==> `ls /usr/local`
PS:上面幾個(gè)%表示法中用{}擴(kuò)住了String,其實(shí)這個(gè){} 只是一種分割符,可以換成別的字符,比如(),那么%表示法就是%(String),當(dāng)然還可以是別的字符,對(duì)于非括號(hào)類型的分割符,左右兩邊要相同, 如%!String!
下面我對(duì)這些表示法簡(jiǎn)單舉幾個(gè)例子:
%{String}用于創(chuàng)建一個(gè)使用雙引號(hào)括起來的字符串
這個(gè)表示法與%Q{String}完全一樣,這邊直接句個(gè)例子看結(jié)果:
復(fù)制代碼 代碼如下:
result = %{hello}
puts "result is: #{result}, Type is:#{result.class}"
結(jié)果: result is: hello, Type is:String
%Q{String}用于創(chuàng)建一個(gè)使用雙引號(hào)括起來的字符串
%q{String}用于創(chuàng)建一個(gè)使用單引號(hào)括起來的字符串
從說明中可以看出這兩個(gè)表示法的區(qū)別就是一個(gè)使用雙引號(hào),一個(gè)使用單引號(hào)。使用雙引號(hào)的字符串會(huì)對(duì)字符串中的變量做較多替換,而單引號(hào)則做較少的替換,具 體看例子。先看%Q{String}:
復(fù)制代碼 代碼如下:
world = "world"
result = %Q{hello #{world}}
puts "result is: #{result}, Type is:#{result.class}"
結(jié)果: result is: hello world, Type is:String
換成%q{String}:
復(fù)制代碼 代碼如下:
world = "world"
result = %q{hello #{world}}
puts "result is: #{result}, Type is:#{result.class}"
結(jié)果:
result is: hello #{world}, Type is:String
從上面的結(jié)果可以看出,較少替換的情況下,#{world}被解析成了字符串,而不會(huì)去計(jì)算這個(gè)變量中的值。
%r{String}用于創(chuàng)建一個(gè)正則表達(dá)式字面值
就像使用/reg/方式一樣,看代碼:
復(fù)制代碼 代碼如下:
result = %r{world}
puts result =~ "hello world"
puts "result is: #{result}, Type is:#{result.class}"
結(jié)果: 6
result is: (?-mix:world), Type is:Regexp
可以看出,world從第6個(gè)字符開始匹配
%w{String}用于將一個(gè)字符串以空白字符切分成一個(gè)字符串?dāng)?shù)組,進(jìn)行較少替換
%W{String}用于將一個(gè)字符串以空白字符切分成一個(gè)字符串?dāng)?shù)組,進(jìn)行較多替換
這兩個(gè)應(yīng)該是大家見過最多的,用這個(gè)方式構(gòu)造數(shù)組,可以省下一些逗號(hào),Ruby真 是會(huì)慣壞大家,以后大家都不用標(biāo)點(diǎn)符號(hào)了。
同樣給一個(gè)簡(jiǎn)單的例子:
復(fù)制代碼 代碼如下:
result = %w{hello world}
puts "result is: #{result}, Type is:#{result.class}, length is:#{result.length}"
結(jié)果: result is: helloworld, Type is:Array, length is:2
%s{String}用于生成一個(gè)符號(hào)對(duì)象
直接先上代碼:
復(fù)制代碼 代碼如下:
result = %s{hello world}
puts "result is: #{result}, Type is:#{result.class}"
sym = :"hello world"
puts "the two symbol is the same: #{sym == result}"
結(jié)果:
result is: hello world, Type is:Symbol
the two symbol is the same: true
可以看出,這兩中方式生成的symbol對(duì)象完全一樣
%x{String}用于執(zhí)行String所代表的命令
比如:
%x{notepad.exe}可以啟動(dòng)windows下的記事本,這里我就不列結(jié)果了(那是一個(gè)大家熟悉的窗口)
您可能感興趣的文章:- 詳解Ruby中正則表達(dá)式對(duì)字符串的匹配和替換操作
- Ruby的字符串與數(shù)組求最大值的相關(guān)問題討論
- Ruby中的字符串編寫示例
- Ruby中操作字符串的一些基本方法
- Ruby中常用的字符串處理函數(shù)使用實(shí)例
- Ruby簡(jiǎn)潔學(xué)習(xí)筆記(一):字符串、數(shù)字、類和對(duì)象
- Ruby中實(shí)現(xiàn)把字符串轉(zhuǎn)換為類的2種方法
- Ruby中字符串左側(cè)補(bǔ)零方法實(shí)例
- Ruby字符串、條件、循環(huán)、數(shù)組、Hash、類基本操作筆記
- Ruby 字符串處理
- Ruby編寫HTML腳本替換小程序的實(shí)例分享