Ruby 中一些百分比符號(%)的用法小結_ruby專題

來源:互聯網
上載者:User

%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, 用於Regex.

複製代碼 代碼如下:

>> %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} 用於建立一個Regex字面值
%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}用於建立一個Regex字面值
就像使用/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下的記事本,這裡我就不列結果了(那是一個大家熟悉的視窗)。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.