Ruby 中一些百分號(%)的用法小結

来源:互联网 发布:装修监理必知 编辑:程序博客网 时间:2024/05/01 14:46

%Q


用於替代雙引號的字元串. 當你需要在字元串里放入很多引號時候, 可以直接用下面方法而不需要在引號前逐個添加反斜杠 (\")

>> %Q(Joe said: "Frank said: "#{what_frank_said}"")=> "Joe said: "Frank said: "Hello!"""

(...)也可用其他非數字字母的符號或成對的符號代替, 諸如[...], !...!, +...+,{...}, <...>等.

以下寫法全部與上面等效:



>> %Q!Joe said: "Frank said: "#{what_frank_said}""!>> %Q[Joe said: "Frank said: "#{what_frank_said}""]>> %Q+Joe said: "Frank said: "#{what_frank_said}""+

除此之外還可省略Q寫作:
>> %/Joe said: "Frank said: "#{what_frank_said}""/=> "Joe said: "Frank said: "Hello!""" 

%q


與%Q類似, 但是表示的是單引號字元串

>> %q(Joe said: 'Frank said: '#{what_frank_said} ' ')=> "Joe said: 'Frank said: '\#{what_frank_said} ' '" 

%W


語法近似於%Q, 用於表示其中元素被雙引號括起的數組.

>> %W(#{foo} Bar Bar\ with\ space)=> ["Foo", "Bar", "Bar with space"] 

%w


用於表示其中元素被單引號括起的數組. 比較奇怪的是\(斜杠空格)會被轉化成(空格), 但是其他的內容不會.

>> %w(a b c\ d \#e #{1}f)=> ["a", "b", "c d", "\\#e", "\#{1}f"]


%x

使用`方法執行一段shell腳本並返回標準輸出內容.

>> %x(echo foo:#{foo})=> "foo:Foo\n" 

%r


語法近似於%Q, 用於正則表達式.

>> %r(/home/#{foo})=> "/\\/home\\/Foo/"


%s

用於表示symbol, 但是不會對其中表達式等內容進行轉化

>> %s(foo)=> :foo>> %s(foo bar)=> :"foo bar">> %s(#{foo} bar)=> :"\#{foo} bar"

%i


Ruby 2.0 之後引入的語法, 用於生成一個symbol數組

2.0.0p247 :014 > %i(a b c)

=> [:a, :b, :c]





附:另一篇




%{String} 用於創建一個使用雙引號括起來的字元串

%Q{String} 用於創建一個使用雙引號括起來的字元串

%Q!Some String of 「Characters」! <==> 」 Some String of /」Characters/」 「

%q{String} 用於創建一個使用單引號括起來的字元串

%q!Some String of 「Characters」! <==> 『Some String of Characters'

%r{String} 用於創建一個正則表達式字面值

%r{/usr/bin/} <==> ///usr//bin///

%w{String} 用於將一個字元串以空白字元切分成一個字元串數組,進行較少替換

%W{String} 用於將一個字元串以空白字元切分成一個字元串數組,進行較多替換

%W(North South East West) <==> ["North", "South", "East", "West"]

%s{String} 用於生成一個符號對象

%x{String} 用於執行String所代表的命令

%x{ ls /usr/local } <==> `ls /usr/local`




PS:上面幾個%表示法中用{}擴住了String,其實這個{} 只是一種分割符,可以換成別的字元,比如(),那麼%表示法就是%(String),當然還可以是別的字元,對於非括弧類型的分割符,左右兩邊要相同, 如%!String!


下面我對這些表示法簡單舉幾個例子:


%{String}用於創建一個使用雙引號括起來的字元串

這個表示法與%Q{String}完全一樣,這邊直接句個例子看結果:

result = %{hello}  puts "result is: #{result}, Type is:#{result.class}"  

結果: result is: hello, Type is:String

%Q{String}用於創建一個使用雙引號括起來的字元串

%q{String}用於創建一個使用單引號括起來的字元串

從說明中可以看出這兩個表示法的區別就是一個使用雙引號,一個使用單引號。使用雙引號的字元串會對字元串中的變數做較多替換,而單引號則做較少的替換,具 體看例子。先看%Q{String}:

world = "world"  result = %Q{hello #{world}}  puts "result is: #{result}, Type is:#{result.class}"

結果: result is: hello world, Type is:String

換成%q{String}:

world = "world"  result = %q{hello #{world}}  puts "result is: #{result}, Type is:#{result.class}"  

結果: result is: hello #{world}, Type is:String

從上面的結果可以看出,較少替換的情況下,#{world}被解析成了字元串,而不會去計算這個變數中的值。


%r{String}用於創建一個正則表達式字面值

就像使用/reg/方式一樣,看代碼:

result = %r{world}  puts result =~ "hello world"  puts "result is: #{result}, Type is:#{result.class}"  

結果: 6 result is: (?-mix:world), Type is:Regexp

可以看出,world從第6個字元開始匹配


%w{String}用於將一個字元串以空白字元切分成一個字元串數組,進行較少替換

%W{String}用於將一個字元串以空白字元切分成一個字元串數組,進行較多替換

這兩個應該是大家見過最多的,用這個方式構造數組,可以省下一些逗號,Ruby真 是會慣壞大家,以後大家都不用標點符號了。

同樣給一個簡單的例子:

result = %w{hello world}  puts "result is: #{result}, Type is:#{result.class}, length is:#{result.length}"  

結果: result is: helloworld, Type is:Array, length is:2

%s{String}用於生成一個符號對象

直接先上代碼:

result = %s{hello world}  puts "result is: #{result}, Type is:#{result.class}"  sym = :"hello world"  puts "the two symbol is the same: #{sym == result}"  

結果:
result is: hello world, Type is:Symbol
the two symbol is the same: true

可以看出,這兩中方式生成的symbol對象完全一樣


%x{String}用於執行String所代表的命令

比如:

%x{notepad.exe}可以啟動windows下的記事本,這裡我就不列結果了(那是一個大家熟悉的窗口)。

0 0
原创粉丝点击