- 浏览: 287727 次
- 性别:
- 来自: 武汉
文章分类
最新评论
-
masuweng:
如何给新人机会 -
masuweng:
多sql结果集按列合并新结果报表实现方案 -
Ahe:
赞
坚持长跑方能赢 -
masuweng:
好好好
程序员如何更好的了解自己所做的事情 -
小楠人:
laoguan123 写道楼主好,使用过一些excel导入导出 ...
excell导入导出
[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会解析
我们看个简单的例子,针对字符串 #{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"
发表评论
-
git仓库创建
2020-09-04 15:33 647推送现有文件夹 cd existing_folder git ... -
puma高并发
2020-08-19 09:31 437nginx突发大量502报错 top看一下,cpu的占用并不高 ... -
searchkick
2019-04-10 11:30 0# 通用查询块(条件) def general_ ... -
导入线下excell业务数据按权重匹配线上数据
2019-03-07 11:00 831业务场景:(系统间还没有接口对调,订单号暂时需要线下处理) 线 ... -
两对象同时映射一对一和一对多
2019-02-20 10:14 752class Kpi::Team < Applicat ... -
ruby一些类加载方式
2018-12-21 10:12 531require_dependency 'order/sco ... -
基于ruby的gem remotipart的异步上传文件
2018-12-21 10:11 497针对某一对象保存实例化之前,异步上传图片保存。 gem ' ... -
基于html2canvas的长图分享
2018-12-21 10:11 1115<span class="ui label ... -
rails处理上传读取excell&生成excell
2018-12-20 14:15 869gem 'spreadsheet' gem 'roo', ... -
基于ruby Mechanize的爬虫
2018-12-20 13:09 599def self.sang_carwler ... -
一些常用加密方式
2018-12-20 13:02 693sign = OpenSSL::Digest::SHA256. ... -
ruby 调用restful接口示例
2018-12-20 12:02 882链接参数中添加token def self.query_p ... -
rails错误日志记录
2018-12-19 14:41 675Rails中对日志的处理采用的是“消息-订阅”机制,各部分组件 ... -
railsAPI接收Base64文件
2018-12-18 11:05 1000tmp_dir = " ... -
ruby 调用savon接口示例
2018-12-18 10:51 938例子一 module Api module Aob ... -
关于国际商城现货展示与购物车的费用设计
2018-11-15 18:34 398关于国际商城现货展示 ... -
基于多线程的全局变量
2018-10-31 19:50 1067def current_nation def ... -
hash最小值过滤算法
2018-10-31 09:52 1040[["数量","包装" ... -
阿里云裸机部署rails运用
2018-10-08 20:33 1305登录阿里云后首先 sudo apt-get update a ... -
打包订单单据发给货代
2018-09-11 15:43 1145pdf&excell&png # rend ...
相关推荐
Ruby经典系列: exceptional ruby 教你在ruby代码中如何处理异常
前端学 Ruby:熟悉 Ruby 语法
类与对象:理解Ruby中的类和对象的概念,学习如何定义类、创建对象以及使用对象的属性和方法。 继承与多态:掌握Ruby中的继承机制,了解子类如何继承父类的属性和方法,并实现自己的特定功能。同时,学习多态的...
ruby-build - 编译和安装Ruby
在Ruby中构建和使用服务对象的简单方法。 安装 将此行添加到您的应用程序的Gemfile中: gem 'ruby-active_action' , '~> 3.0.1' 或使用其他宝石: 然后执行: $ bundle install ActiveAction示例 # app/actions/...
p6df-Ruby目录p6df-Ruby徽章概括贡献行为守则变化用法p6df-ruby:p6df-ruby / init.zsh: p6_ruby_prompt_info() p6df :: modules :: ruby :: deps() p6df :: modules :: ruby :: home :: symlink() ...
ruby笔记1ruby笔记1ruby笔记1ruby笔记1ruby笔记1ruby笔记1
Learn the principles behind object-oriented programming and within a few chapters create a fully functional Ruby application. You'll also gain a basic understanding of many ancillary technologies such...
RubyLearning:关于Ruby语言的实践
如果任何需要旧版rubyzip的第三方gem出现问题,则可以使用以下解决方法: gem 'rubyzip' , '>= 1.0.0' # will load new rubyzip version gem 'zip-zip' # will load compatibility for old rubyzip API. 要求 Ruby...
S2S:步步为营 Peer 2对等目录共享,用于Ruby(Shoes)片段 介绍 一个简单的p2p应用程序,用于分发少量(免费)Ruby应用程序。 GUI由Green-Shoes制成 P2p部分基于_why显示的旧9行LOC。 所以这个工具是他的纪念品。 ...
目录散列检索键和值将键映射到值将值映射到键重复的枚举键或重复的值遗产贡献版权和许可相关项目 用法枚举可以定义为常量或类方法来访问,这是优先考虑的问题。 常数定义枚举,并将其作为常量引用。 class ...
Ruby语言教程:Ruby 变量示例
Ruby-Launchd-使用Ruby控制Mac OS X服务Ruby-Launchd就像launchctl(或著名的午餐)一样。 它使您可以使用Ruby在Mac OS X上轻松创建,启动和停止服务。安装将此行添加到您的应用程序的Gemfile中: gem 'launchd' ...
从程序所需要的变量、常数、方法、类、控制结构等语法的说明,到类的主要使用方法、简单的应用等,尽可能都浅显地加以解说。您若是自己动手玩过HTML这样的读者,应该就能充分活用《Ruby Programming:向Ruby之父学...
Ruby on Rails的示例API开发 该项目演示了Ruby on Rails上的示例API开发。
一种构建和使用服务对象的简单方法。 访问父宝石。 安装 将此行添加到您的应用程序的Gemfile中: gem 'grape-active_action' , '~> 3.0.6 ' 或使用原始的宝石: gem 'ruby-active_action' , '~> 3.0.6 ' 或使用...
如果您使用的是 Windows 9x,那么请在您的 c:\autoexec.bat 中添加:set PATH="D:\(ruby 安装目录)\bin;%PATH%" Windows NT/2000 用户需要修改注册表。 点击控制面板|系统性能|环境变量。 在系统变量下,选择...
林鲁比描述RinRuby是一个Ruby库,它将R解释器集成到Ruby中,从而使R的统计例程和图形在Ruby中可用。 该库包含一个简单的Ruby脚本,该脚本易于安装,并且不需要任何特殊的R编译或安装。由于该库是100%纯Ruby,因此可...
Programming Ruby 第二版(Second Edition)英文版于2005年出版,之后有了中文版。但可惜的是网上流传的中文版PDF基本上都是扫描版的,文字内容得自己打出来,看到好的代码也得自己动手,无法Ctrl-C + Ctrl-V,还...