[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 ...
随机推荐
- Android 使用SWIG生成Jni代码<转>
http://blog.csdn.net/u010780612/article/details/51066819 感觉也就那样把,我没有编译成功,实用性暂时没看到多大优点...
- AOP基础-JDK动态代理
动态代理技术就是用来产生一个目标对象的代理对象的,代理对象应与目标对象(被代理的对象)有相同的方法,实现对目标对象访问的拦截,并增强目标对象的一些功能,而不需要目标对象去做任何的更改,使得目标对象有更 ...
- 一个servlet处理多个请求(使用Method的反射机制)
方法一 可以通过在请求的时候加上参数,然后在servlet中获取请求的参数,再去调用对应的方法.达到一个servlet处理多个请求的目的 test.jsp: <%@ page language= ...
- HTML5 学习指导
HTML 语义 HTML5为我们提供了很多旨在精确描述内容的语义元素.确保你可以从它丰富的词汇中获益. <!-- bad --> <div id="main"&g ...
- 【总结整理】openlayer
实时路况 http://www.cnblogs.com/gisvip/archive/2012/11/24/2787141.html
- POJ 3714 分治/求平面最近点对
第一次见这种问题直接懵圈...没想到分治法这么强大,借鉴了lyd的代码: 代码如下 #include<cstdio> #include<algorithm> #include& ...
- 利用General框架开发RDLC报表
RDLC是微软推出的自家的报表软件,虽然没有一些第三方的报表软件强大好用,但是作为VisualStudio集成的报表工具,在客户要求不高的情况下还是非常值得一用的,本文将介绍通过General代码生成 ...
- Ubuntu相关IP配置(转)
配置文件:/etc/network/interfaces 打开后里面可设置DHCP或手动设置静态ip.前面auto eth0,让网卡开机自动挂载. 1. 以DHCP方式配置网卡 编辑文件/etc/ne ...
- SSH2+proxool 出现No suitable driver found for proxool.mysqlProxool
SSH2+proxool 出现No suitable driver found for proxool.mysqlProxool 首先我们要明确使用的是SSH2框架,然而Struts2是基于filte ...
- 关于IO流---笔记1
今日内容介绍1.File2.递归=================================================================================1 I ...