单引号内的内容,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 的用法的更多相关文章

  1. 【转载】Java中的回车换行符/n /r /t

    source:http://hane00.blog.163.com/blog/static/1600615220126204446809/ '\r'是回车,'\n'是换行,前者使光标到行首,后者使光标 ...

  2. Angular中的$q的形象解释及深入用法

    作者:寸志链接:https://zhuanlan.zhihu.com/p/19622332来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 早上,老爸说:“儿子,天气如何 ...

  3. 在Django中使用Q()对象

    转载于:  http://www.smallerpig.com/1000.html 问题 一般我们在Django程序中查询数据库操作都是在QuerySet里进行进行,例如下面代码: >>& ...

  4. 形象的讲解angular中的$q与promise(转)

    以下内容摘自http://www.ngnice.com/posts/126ee9cf6ddb68 promise不是angular首创的,作为一种编程模式,它出现在……1976年,比js还要古老得多. ...

  5. Solr中的q与fq参数的区别

    转自:搜索系统5:Solr中的q与fq参数的区别在那儿 1.对结果排序有影响 今天遇到一个问题,把相同的参数比如name:张三,放到q与fq,两者返回的结果完全不一样. 经过debug发现,原因是这两 ...

  6. 原创:形象的讲解angular中的$q与promise

    promise不是angular首创的,作为一种编程模式,它出现在……1976年,比js还要古老得多.promise全称是 Futures and promises.具体的可以参见 http://en ...

  7. angular中的$q服务实例

    用于理解$q服务 参考:http://www.zouyesheng.com/angular.html#toc39 广义回调管理 和其它框架一样, ng 提供了广义的异步回调管理的机制. $http 服 ...

  8. Ruby(或cmd中)输入命令行编译sass

    Ruby(或cmd中)输入命令行编译sass步骤如下: 举例: 1.在F盘中新建一个总文件夹,比如test文件夹,其中在该文件夹下面建立html.images.js.sass等文件夹. 2.在sass ...

  9. ruby for in 循环中改变i的值无效

    ruby for in 循环中改变i的值无效 for j in 1..5 puts "#{j}hehe" j = j + 2 #break end 在循环中,使用j = j + 2 ...

随机推荐

  1. Struts旅程(六)Struts页面转发控制ActionForward和ActionMapping

    转自:https://blog.csdn.net/lovesummerforever/article/details/19125933

  2. ios下编译opencv

    如果想要在ios下编译opencv 需要安装Cmake 这里通过homebrew 来安装cmake ios下打开终端然后先安装 homebrew :(mac 下自带ruby) ruby -e &quo ...

  3. matlab读取excel

    xlsread函数: x = xlsread('d:/min1.csv','B2:B10');    %文件名和路径:所读取的数据范围:

  4. solr4.8中集成mmseg4j1.9.1

    要想在Solr中整合mmseg4j其实很容易,只需要如下几个步骤 1.下载(https://code.google.com/p/mmseg4j/downloads/list)并解压mmseg4j-1. ...

  5. 第5章 选举模式和ZooKeeper的集群安装 5-1 集群的一些基本概念

    xx就是我们的master,也就是我们的主节点.心跳机制,当有一个节点挂掉之后,整个集群还是可以工作的.选举模式,我们现在的master是正常运行的,但是在某些情况下它宕机了死机了,那么这个时候它这个 ...

  6. 关联映射、关联查询【重点掌握一条SQL语句的那种方法】

    1 什么叫关联映射 通过数据库对象之间的关联关系(一对一.一对多.多对多),反映到实体对象上之间的引用. 举例 用户实体类(User):user_id user_name user_token 笔记本 ...

  7. iOS 通过接受距离传感器的消息改变屏幕的明暗度(仅限用于真实的手机)

    #import "AppDelegate.h" @interface AppDelegate () @end @implementation AppDelegate - (BOOL ...

  8. VS2013中,将Qt的GUI程序改为控制台程序

    在Visual studio 中创建QT GUI程序是不带Console的,但是调试时候常常需要查看打印信息,可以通过如下设置显示控制台 方法一.在vs中直接创建控制台程序方法二.当你通过设置你的应用 ...

  9. p2114 起床困难综合症

    传送门 分析 orz zwj 最好想到的方法是我们枚举每一位是0还是1,然后暴力求出经过n个操作之后的结果来决定这一位是0还是1 然后我们发现这种暴力的做法居然能a 但是还有更好的方法 我们只考虑开始 ...

  10. Entity Framework Tutorial Basics(15):Querying with EDM

    Querying with EDM: We have created EDM, DbContext, and entity classes in the previous sections. Here ...