[Ruby]转载: 关于ruby中 %Q, %q, %W, %w, %x, %r, %s 的用法
单引号内的内容,ruby会原样输出
双引号内的内容,ruby会解析
我们看个简单的例子,针对字符串 #{foo}test 我们分别用单引号核双引号操作
'#{foo}test' => "\#{foo}test" |
"#{foo}test" NameError: undefined local variable or method `foo' for main: Object from (irb): 4 from C :/Ruby192/bin/irb: 12 :in `<main>' |
显而易见, 双引号对字符串内的#进行了解析, 导致ruby认为foo是个方法,出错.
类似的, %W %Q 相当于双引号, %w %q 相当于单引号.
转载: http://simpleror.wordpress.com/2009/03/15/q-q-w-w-x-r-s/
%Q
This is an alternative for double-quoted strings, when you have more quote characters in a string.Instead of putting backslashes in front of them, you can easily write:>> %Q(Joe said: "Frank said: "#{what_frank_said}"")
=> "Joe said: "Frank said: "Hello!"""
The parenthesis “(
…)
” can be replaced with any other non-alphanumeric characters and non-printing characters (pairs), so the following commands are equivalent:>> %Q!Joe said: "Frank said: "#{what_frank_said}""!
>> %Q[Joe said: "Frank said: "#{what_frank_said}""]
>> %Q+Joe said: "Frank said: "#{what_frank_said}""+
You can use also:>> %/Joe said: "Frank said: "#{what_frank_said}""/
=> "Joe said: "Frank said: "Hello!"""
%q
Used for single-quoted strings.The syntax is similar to %Q, but single-quoted strings are not subject to expression substitution or escape sequences.>> %q(Joe said: 'Frank said: '#{what_frank_said} ' ')
=> "Joe said: 'Frank said: '\#{what_frank_said} ' '"
%W
Used for double-quoted array elements.The syntax is similar to %Q>> %W(#{foo} Bar Bar\ with\ space)
=> ["Foo", "Bar", "Bar with space"]
%w
Used for single-quoted array elements.The syntax is similar to %Q, but single-quoted elements are not subject to expression substitution or escape sequences.>> %w(#{foo} Bar Bar\ with\ space)
=> ["\#{foo}", "Bar", "Bar with space"]
%x
Uses the `
method and returns the standard output of running the command in a subshell.The syntax is similar to %Q.>> %x(echo foo:#{foo})
=> "foo:Foo\n"
%r
Used for regular expressions.The syntax is similar to %Q.>> %r(/home/#{foo})
=> "/\\/home\\/Foo/"
%s
Used for symbols.It’s not subject to expression substitution or escape sequences.>> %s(foo)
=> :foo
>> %s(foo bar)
=> :"foo bar"
>> %s(#{foo} bar)
=> :"\#{foo} bar"
[Ruby]转载: 关于ruby中 %Q, %q, %W, %w, %x, %r, %s 的用法的更多相关文章
- 【转载】Java中的回车换行符/n /r /t
source:http://hane00.blog.163.com/blog/static/1600615220126204446809/ '\r'是回车,'\n'是换行,前者使光标到行首,后者使光标 ...
- Angular中的$q的形象解释及深入用法
作者:寸志链接:https://zhuanlan.zhihu.com/p/19622332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 早上,老爸说:“儿子,天气如何 ...
- 在Django中使用Q()对象
转载于: http://www.smallerpig.com/1000.html 问题 一般我们在Django程序中查询数据库操作都是在QuerySet里进行进行,例如下面代码: >>& ...
- 形象的讲解angular中的$q与promise(转)
以下内容摘自http://www.ngnice.com/posts/126ee9cf6ddb68 promise不是angular首创的,作为一种编程模式,它出现在……1976年,比js还要古老得多. ...
- Solr中的q与fq参数的区别
转自:搜索系统5:Solr中的q与fq参数的区别在那儿 1.对结果排序有影响 今天遇到一个问题,把相同的参数比如name:张三,放到q与fq,两者返回的结果完全不一样. 经过debug发现,原因是这两 ...
- 原创:形象的讲解angular中的$q与promise
promise不是angular首创的,作为一种编程模式,它出现在……1976年,比js还要古老得多.promise全称是 Futures and promises.具体的可以参见 http://en ...
- angular中的$q服务实例
用于理解$q服务 参考:http://www.zouyesheng.com/angular.html#toc39 广义回调管理 和其它框架一样, ng 提供了广义的异步回调管理的机制. $http 服 ...
- Ruby(或cmd中)输入命令行编译sass
Ruby(或cmd中)输入命令行编译sass步骤如下: 举例: 1.在F盘中新建一个总文件夹,比如test文件夹,其中在该文件夹下面建立html.images.js.sass等文件夹. 2.在sass ...
- ruby for in 循环中改变i的值无效
ruby for in 循环中改变i的值无效 for j in 1..5 puts "#{j}hehe" j = j + 2 #break end 在循环中,使用j = j + 2 ...
随机推荐
- 12-21C#电脑蓝屏效果(可以恶搞整人哦)、输入输出流(StreamReader/streamWriter)
一.winform电脑蓝屏效果 第一种方法:基本操作: 第一步:创建一个新的C#窗体“Form1”: 第二步:在设计窗口中,更改其属性: 1)text属性:将form1的text属性中的文字取消掉,然 ...
- 问题:c# json解析;结果:c# 解析JSON的几种办法
c# 解析JSON的几种办法 欲成为海洋大师,必知晓海中每一滴水的真名. 刚开始只是想找一个转换JSON数组的方法,结果在MSDN翻到一大把. 搜索过程中免不了碰到一大堆名词:WCF => Da ...
- 用UltraISO把硬盘文件制作成ISO格式
转自:https://wenku.baidu.com/view/0052c88dcc22bcd126ff0cbf.html 用UltraISO把硬盘文件制作成ISO格式方法: 制作硬盘ISO文件步骤一 ...
- 关于EF中实体和数据表以及查询语句映射的问题
关于EF中实体和数据表以及查询语句映射的问题? 很多人在使用的时候分不清楚 实体字段应该少于等于(数据库中的表字段或者SQL查询中的临时字段).这样在查询或者添加修改都不会出现问题 如果实体的字段大于 ...
- [Python Study Notes]七彩散点图绘制
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- 【译】Android 数据库 ORMLite
@DatabaseField cloumnName:指定字段名,不指定则变量名作为字段名 canBeNull:是否可以为null dataType:指定字段的类型 defaultValue:指定 ...
- final 子类禁止重写
<?php //子类中编写和父类中完全一样的函数,是对父类中的函数进行重写 class BaseClass{ public function test() { echo "BaseCl ...
- nginx显示目录下面的文件
location / { root /data/source; index index.html index.htm; autoindex on; #autoindex指令,显示文件目录下面的 ...
- python3-打印一个进度条
# Auther: Aaron Fan import sys,time for i in range(30): #打印一个#号,这种方法打印不会自动换行 sys.stdout.write('#') # ...
- input 输入框两种改变事件的方式
一.在输入框内容变化的时候不会触发,当鼠标在其他地方点一下才会触发 $('input[name=myInput]').change(function() { ... }); 二.在输入框内容变化的时候 ...