Ruby中的%表示法
2014-05-20 18:31
260 查看
%{String} 用于创建一个使用双引号括起来的字符串,这个表示法与%Q{String}完全一样
%Q{String} 用于创建一个使用双引号括起来的字符串
%q{String} 用于创建一个使用单引号括起来的字符串
从说明中可以看出这两个表示法的区别就是一个使用双引号,一个使用单引号。使用双引号的字符串会对字符串中的变量做较多替换,而单引号则做较少的替换。
%r{String} 用于创建一个正则表达式字面值
%w{String} 用于将一个字符串以空白字符切分成一个字符串数组,进行较少替换
%W{String} 用于将一个字符串以空白字符切分成一个字符串数组,进行较多替换
%s{String} 用于生成一个符号对象
%x{String} 用于执行String所代表的命令
比如: %x{notepad.exe}可以启动windows下的记事本
PS:上面几个%表示法中用{}扩住了String,其实这个{}只是一种分割符,可以换成别的字符,比如(),那么%表示法就是%(String),当然还可以是别的字符,对于非括号类型的分割符,左右两边要相同,如%!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!
相关文章推荐
- Ruby中的百分号表示法
- Ruby中的%表示法
- Ruby on Rails 入门之:(3) Ruby中的多种字符串表示方法
- Ruby中的%表示法(百分号表示法)
- Ruby中的百分号表示法
- Ruby 多种字符串表示方式
- ruby随机在控制台显示字符串
- [计算机] -32768~32767 计算机中的 -1 表示
- 矩阵的压缩存储(稀疏矩阵的十字链表存储、稀疏矩阵的三元组行逻辑链接的顺序表存储表示、稀疏矩阵的三元组顺序表存储表示)
- 程序的机器级表示
- MySQL数据库 小数用百分比表示语法
- 二进制表示(转)
- RGB及ARGB中16进制的表示与转换方法
- linux终端下,使用ls时,显示的文件颜色,都表示什么含义,在哪里可以查看和修改
- 【js类库Raphaël】基于svg中的path动态绘制40%表示的环型图
- ZOJ - 1729(最小表示法。 <<然后输出字典序最小
- 数组的顺序存储表示
- Ruby 字符串处理 详细出处参考:http://www.jb51.net/article/12863.htm
- 浮点数的二进制表示学习笔记
- windows下把ruby脚本转换成exe