当前位置: 首页 > news >正文

转载: 关于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"

转载于:https://www.cnblogs.com/ilazysoft/archive/2011/09/22/2184701.html

相关文章:

  • django专题—安装、创建项目、添加应用
  • 自定义的asp.net翻页控件
  • python 数学运算符
  • 标题一定要长~~~~长~~~~~~~~~~~~~~长~~~~~~~~
  • python 中set模块的用法
  • Turbo C 2.0集成开发环境的使用
  • Ajax on Rails 2. The Eras of Web Development
  • 创建按钮的两种方法
  • JavaScript对象知识点总结
  • 在Python常用模块I如何打开相关文件的方法
  • 开发各种信息管理系统的标准演示数据都帮你整理好了,少了录入演示\测试\模拟数据的烦恼了[提供下载]...
  • Web2.0工具箱
  • Lync2013 恢复-整残之后如何重新安装
  • js获得浏览器相关参数【转】
  • ASP.NET 安全认证(三)
  • php的引用
  • [译] 理解数组在 PHP 内部的实现(给PHP开发者的PHP源码-第四部分)
  • 《Javascript数据结构和算法》笔记-「字典和散列表」
  • 4月23日世界读书日 网络营销论坛推荐《正在爆发的营销革命》
  • conda常用的命令
  • egg(89)--egg之redis的发布和订阅
  • js正则,这点儿就够用了
  • Redash本地开发环境搭建
  • SAP云平台运行环境Cloud Foundry和Neo的区别
  • XML已死 ?
  • yii2中session跨域名的问题
  • 关于for循环的简单归纳
  • 记录一下第一次使用npm
  • 快速体验 Sentinel 集群限流功能,只需简单几步
  • 面试题:给你个id,去拿到name,多叉树遍历
  • 什么是Javascript函数节流?
  • 想使用 MongoDB ,你应该了解这8个方面!
  • 你对linux中grep命令知道多少?
  • Unity3D - 异步加载游戏场景与异步加载游戏资源进度条 ...
  • #13 yum、编译安装与sed命令的使用
  • (2022版)一套教程搞定k8s安装到实战 | RBAC
  • (4) PIVOT 和 UPIVOT 的使用
  • (cos^2 X)的定积分,求积分 ∫sin^2(x) dx
  • (三)模仿学习-Action数据的模仿
  • (学习日记)2024.03.12:UCOSIII第十四节:时基列表
  • (转)一些感悟
  • *(长期更新)软考网络工程师学习笔记——Section 22 无线局域网
  • .dwp和.webpart的区别
  • .NET CF命令行调试器MDbg入门(三) 进程控制
  • .NET MAUI学习笔记——2.构建第一个程序_初级篇
  • .net mvc actionresult 返回字符串_.NET架构师知识普及
  • .Net Web窗口页属性
  • .NET 指南:抽象化实现的基类
  • .NET(C#、VB)APP开发——Smobiler平台控件介绍:Bluetooth组件
  • .net项目IIS、VS 附加进程调试
  • .net中调用windows performance记录性能信息
  • @CacheInvalidate(name = “xxx“, key = “#results.![a+b]“,multi = true)是什么意思
  • @NestedConfigurationProperty 注解用法
  • @ResponseBody
  • [ C++ ] 继承