ruby 字符串学习2
在一个ruby字符串中包含表但是或者变量。想使用不同的值替换表达式或者变量
1 类似java 或者python的printf-style方式
template = 'Oceania has always been at war with %s.'
template % 'Eurasia' # => "Oceania has always been at war with Eurasia."
template % 'Eastasia' # => "Oceania has always been at war with Eastasia."
'To 2 decimal places: %.2f' % Math::PI # => "To 2 decimal places: 3.14" 'Zero-padded: %.5d' % Math::PI # => "Zero-padded: 00003"
2 或者使用erb,因为我用的ide,所以最后一行使用的是kernel.binding ,如果你在irb中,可以只使用binding
require 'erb'
template = ERB.new %q{Chunky <%= food %>!}
food = "bacon"
puts output = template.result(Kernel.binding)
3翻转字符串
s = ".sdrawkcab si gnirts sihT"
s.reverse # => "This string is backwards."
s # => ".sdrawkcab si gnirts sihT" s.reverse! # => "This string is backwards."
s # => "This string is backwards."
s = "order. wrong the in are words These"
s.split(/(\s+)/).reverse!.join('') # => "These words are in the wrong order." s.split(/\b/).reverse!.join('') # => "These words are in the wrong. order"
(\s+)和\s+区别,(\s+)匹配的空格会包含返回列表中
"Three little words".split(/\s+/)
# => ["Three", "little", "words"]
"Three little words".split(/(\s+)/)
# => ["Three", " ", "little", " ", "words"]
ruby 字符串学习2的更多相关文章
- ruby 字符串学习笔记1
1 从一种数据结构中构件字符串 hash = { key1: "val1", key2: "val2" } string = "" hash ...
- ruby字符串学习笔记5
1获取字符串某部分 s = "My kingdom for a string!" s.slice(3,7) # kingdom s[3,7] # kingdom s[/.ing/] ...
- ruby字符串学习笔记4
1 单独处理字符串的字符 如果处理的是ASCII码的文档使用string#each_byte 注意 没有 string#each方法,String#each_byte 速度比 String#scan快 ...
- ruby 字符串学习笔记3
ascii转字符或者字符串转ascii "a".ord # => 97 "!".ord # => 33 "\n".ord # = ...
- 雷林鹏分享:Ruby 字符串(String)
Ruby 字符串(String) Ruby 中的 String 对象存储并操作一个或多个字节的任意序列,通常表示那些代表人类语言的字符. 最简单的字符串是括在单引号(单引号字符)内.在引号标记内的文本 ...
- Ruby字符串(1):String基本用法
String字符串 字符串由String类提供,除了直接使用单双引号或其它字面量创建字符串,也可以使用String.new()方法来创建. a = "hello" b = Stri ...
- ruby 字符串常用方法学习
引用链接:http://www.blogjava.net/nkjava/archive/2010/01/03/308088.html 1,切片:silce, [ ]-----------------[ ...
- ruby -- 基础学习(八)中文字符串截取的函数
学习来源:http://www.codesky.net/article/200910/166595.html truncate(text, length = 30, truncate_string = ...
- Ruby Rails学习中:User 模型,验证用户数据
用户建模 一. User 模型 实现用户注册功能的第一步是,创建一个数据结构,用于存取用户的信息. 在 Rails 中,数据模型的默认数据结构叫模型(model,MVC 中的 M).Rails 为解决 ...
随机推荐
- OSGi 的核心配置、动态化及问题
一.OSGi的核心组件Bundle,与java中jar包的差别就是元数据配置: 常用的Bundle元数据定义: a) Bundle-Activator:定义Activator的实现全 ...
- Unity3D研究院编辑器之脚本获取资源内存和硬盘大小
内存 使用Profiler可以查看某个资源的内存占用情况,但是必须启动游戏,并且待查看的资源已经载入游戏中.我希望的是不启动游戏,也能看到它的内存好做统计. 硬盘 由于unity中的资源压缩格式记录在 ...
- PgSQL · 追根究底 · WAL日志空间的意外增长
问题出现 我们在线上巡检中发现,一个实例的pg_xlog目录,增长到4G,很是疑惑.刚开始怀疑是日志归档过慢,日志堆积在pg_xlog目录下面,未被清除导致.于是检查归档目录下的文件,内容如下.但发现 ...
- 【转载】关于Python中的yield
在介绍yield前有必要先说明下Python中的迭代器(iterator)和生成器(constructor). 一.迭代器(iterator) 在Python中,for循环可以用于Python中的任何 ...
- 元组的cmp()内建函数
>>> list1,list2=[,,'abc'] >>> cmp(list1,list2) - >>> cmp(list2,list1) > ...
- 阿里云安装nginx 和 php-fpm
yum install yum-priorities -y rpm -Uvh http://download.fedora.redhat.com/pub/epel/5/i386/epel-releas ...
- 【转】svn服务器IP修改后,本地怎么跟新svn同步,svn relocate 操作
本文来源:http://www.kukaka.org/home/showonews/444 1.进入工作复本 cd ~/test 2.查看仓库地址(URL) svn info 路径: . URL ...
- JAVA获取时间戳,哪个更快
目前获取毫秒值大概有下面三种方法 //方法 一 System.currentTimeMillis(); //方法 二 Calendar.getInstance().getTimeInMillis(); ...
- PEM文件格式详细解析
PEM文件格式存档 Author:Roson sun sunxiao@tomonline-inc.com Time:2006-4-11 1. 描述: Openssl使用PEM(RFC 1421-14 ...
- javascript url 相关函数(escape/encodeURL/encodeURIComponent区别)
JS获取url参数及url编码.解码 完整的URL由这几个部分构成:scheme://host:port/path?query#fragment ,各部分的取法如下: window.location. ...