%{String}  用于创建一个使用双引号括起来的字符串,这个表示法与%Q{String}完全一样

result = %{hello}
puts "result is: #{result}, Type is:#{result.class}" #>>result is: hello, Type is: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 world = "world"
result = %q{hello #{world}}
puts "result is: #{result}, Type is:#{result.class}"
#>>result is: hello #{world}, Type is:String

%r{String} 用于创建一个正则表达式字面值

result = %r{world}
puts result =~ "hello world"
puts "result is: #{result}, Type is:#{result.class}" #>>6
#>>result is: (?-mix:world), Type is:Regexp

%w{String} 用于将一个字符串以空白字符切分成一个字符串数组,进行较少替换 
    %W{String} 用于将一个字符串以空白字符切分成一个字符串数组,进行较多替换

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

%x{String} 用于执行String所代表的命令

比如: %x{notepad.exe}可以启动windows下的记事本

  PS:上面几个%表示法中用{}扩住了String,其实这个{}只是一种分割符,可以换成别的字符,比如(),那么%表示法就是%(String),当然还可以是别的字符,对于非括号类型的分割符,左右两边要相同,如%!String!

最新文章

  1. 支付宝Andfix 原理解析
  2. 3.使用CXF开发webService
  3. spring 源代码地址
  4. CGRectGet系列
  5. 【转载】【JQuery学习】jQuery插件开发
  6. jquery 中如何将数组转化为json字符串,然后再转化回来?
  7. 实用项目管理前台框架:EasyUI,ExtJs
  8. 教你把UIView切成任意形状
  9. leetcode — combinations
  10. 高可用Hadoop平台-Ganglia安装部署
  11. [UE4]在蓝图中设置图片
  12. Windows服务设置
  13. Hadoop框架
  14. runloop - 介绍
  15. 对视频播放软件KMplayer的评价
  16. 在Django中Session的那点事!
  17. tcp与http的区别
  18. tomcat启动了server.xml中没有配置的项目
  19. a=a+1背后的内存模型和CPU高速缓存
  20. BZOJ - 5427:最长上升子序列 (二分&思维)

热门文章

  1. hbase系列
  2. 【HDU - 4341】Gold miner(分组背包)
  3. python学习日记(匿名函数)
  4. 【题解】 bzoj3916: [Baltic2014]friends (字符串Hash)
  5. 【bfs】拯救少林神棍(poj1011)
  6. poj3680 Intervals (费用流)
  7. poj2373 Dividing the Path (单调队列+dp)
  8. NOIP2018保卫王国
  9. centos7安装java环境和maven环境
  10. 【洛谷P2042】维护数列