单引号内的内容,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. leetcode653

    class Solution { public: bool findTarget(TreeNode* root, int k) { queue<TreeNode> Q; vector< ...

  2. 来自T00ls的帖子-XSS的奇技淫巧

    T00LS在前段时间开启了markdown支持,这个漏洞也正是markdown的问题导致. Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定 ...

  3. Android 音频播放分析笔记

    AudioTrack是Android中比较偏底层的用来播放音频的接口,它主要被用来播放PCM音频数据,和MediaPlayer不同,它不涉及到文件解析和解码等复杂的流程,比较适合通过它来分析Andro ...

  4. eclipse中maven的配置与使用

    以eclipse Juno版本为例 1.插件安装 eclipse==>help====>install new software===>add name :m2e location: ...

  5. oracle --(一)数据块(data Block)

    基本关系:数据库---表空间---数据段---分区---数据块 数据块(data Block)一.数据块Block是Oracle存储数据信息的最小单位.这里说的是Oracle环境下的最小单位.Orac ...

  6. C#正则表达式匹配双引号

    html: <img class="bubble large" src="/images/hero-logos/cog.svg" width=" ...

  7. 一个小错误,在for循环中,每次repaint()的时候,记得先把frame涂成白色的。等于擦掉原来的痕迹·。

    import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Animate { int x=1; in ...

  8. vue 之 let 和const

    浏览目录 let const let es6新增了let命令,用来声明变量.它的用法类似于var,但是所声明的变量,只在let命令所在的代码块内有效. 上面代码在代码块之中,分别用let和var声明了 ...

  9. oracle获取列的备注和数据类型

    select column_name, data_type, data_precision, data_scale, nvl((select t_s.comments from all_col_com ...

  10. swing重绘按钮为任意形状图案的方法

    swing重绘按钮为任意形状图案的方法 摘自https://www.jb51.net/article/131290.htm 转载  更新时间:2017年12月22日 13:43:00   作者:_Th ...