ruby 字符串学习笔记1
1 从一种数据结构中构件字符串
hash = { key1: "val1", key2: "val2" }
string = ""
hash.each { |k,v| string << "#{k} is #{v}\n" }
puts string
# key1 is val1
# key2 is val2
变种
string = ""
hash.each { |k,v| string << k.to_s << " is " << v << "\n" }
更高效办法使用 Array#join
puts hash.keys.join("\n") + "\n"
# key1
# key2
或者
puts hash.keys.join("")
# key1key2
2 创建一个包含ruby变量或者表达式的字符串
number = 5
"The number is #{number}."# => "The number is 5."
"The number is #{5}."# => "The number is 5."
"The number after #{number} is #{number.next}."# => "The number after 5 is 6."
"The number prior to #{number} is #{number-1}."# => "The number prior to 5 is 4."
"We're ##{number}!"# => "We're #5!"
也可以这样使用但不要这么做
%{Here is #{class InstantClass
def bar
"some text"
end
end
InstantClass.new.bar
}.}
# => "Here is some text."
here document使用
name = "Mr. Lorum"
email = <<END
Dear #{name},
Unfortunately we cannot process your insurance claim at this
time. This is because we are a bakery, not an insurance company.
Signed,
Nil, Null, and None
Bakers to Her Majesty the Singleton
END # => "Dear Mr. Lorum,\nUnfortunately we cannot process your insurance claim at this\ntime. This is because we are a bakery, not an insurance company.\nSigned,\nNil, Null, and None\nBakers to Her Majesty the Singleton\n"
<<end_of_poem
There once was a man from Peru
Whose limericks stopped on line two
end_of_poem
# => "There once was a man from Peru\nWhose limericks stopped on line two\n"
ruby 字符串学习笔记1的更多相关文章
- 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 # = ...
- Swift 2.0 字符串学习笔记(建议掌握OC字符串知识的翻阅)
自己公司开现在使用OC语言在写,但Swift似乎是苹果更推荐使用的开发语言,估计也是未来开发的趋势,自己以前有接触swift,但又由于公司的项目赶,也没有时间去好好地学习这款开发语言.现在年底了,项目 ...
- ruby语言学习笔记2
ruby学习笔记2 (摘自<ruby程序设计语言教程(中文版)>.pdf,全书25页) 1.同一个问题ruby有多个解决方案,途径 ruby之父:松本行弘(Matz),1993年创立 r ...
- ruby 字符串学习2
在一个ruby字符串中包含表但是或者变量.想使用不同的值替换表达式或者变量 1 类似java 或者python的printf-style方式 template = 'Oceania has alway ...
- ruby编程语言-学习笔记5(第5章 语句和控制结构)
以下是2种表达方式一样. if expression code end if expression then #推荐这种形式 code end expression的值不是false或nil,则cod ...
- ruby编程语言-学习笔记4(第4章 表达式和操作符)
4.6.9 范围 Flip-Flops: ..和... ..和... 操作符不是基于方法的,无法重定义.(优先级比较低) x+1 .. x*x #可以认为是x+1 至 x*x 的范围 因为操作 ...
- ruby编程语言-学习笔记2(第4章 表达式和操作符)
对属性和数组元素的赋值 o.m + = 1 ===>o.m = (o.m()+ 1) # 对 o.m()这个方法的结果+1 对数组元素的赋值也是通过 方法调用来完成. 0.[] = 1 == ...
随机推荐
- 关于margin和padding的总结
总结一下: 要想实现如(图一)效果,(即一个div中的子元素与父元素有间距): 如果类名为.middle的父元素没有写border,则类名为firstChild的子元素设置margin-top,会导致 ...
- PHP 文件迭代器
使用了SPL的 迭代器, 可以直接对打开的文件进行foreach读取, 类的构造如下 class fileIterator implements Iterator { private $fp; pri ...
- 虚拟化之vmware-vsphere概念,原理,功能
080-login-back.vmx .encoding = "UTF-8"config.version = "8"virtualHW.version = &q ...
- jQuery Mobile_事件
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- discuz X2.0教程]教你快速了解Discuz!程序文件功能,修改文件从此不用再求人
x3.x数据字典 http://faq.comsenz.com/library/database/x3/x3_index.htm 先从根目录开始,根目录文件一般都是入口,即执行具体功能的代码一般不在这 ...
- 数据导出至Excel文件--好库编程网http://code1.okbase.net/codefile/SerializeHelper.cs_2012122018724_118.htm
using System; using System.IO; using System.Data; using System.Collections; using System.Data.OleDb; ...
- 多线程编程之Windows环境下创建新线程
转自: http://www.cnblogs.com/lgxqf/archive/2009/02/10/1387480.html 在 Win32 API 中,创建线程的基本函数是 CreateThre ...
- lucene.net 使用过程中的 几个注意事项(含termquery 和QueryParser 的区别)
几个注意事项1.建立索引时 插入的顺序(不设置document和字段的boost) 会影响到 查询结果的默认排序,建议:将最新生成的文章 最后建索引 这样 查询结果首先显示的是 最后插入的数据2.Bo ...
- 读书笔记:Ross:概率模型导论:方差和协方差
例2.34 考虑N个人,一些人赞同某个提议,另一些人反对.假定Np个人赞同,N-Np人反对,p未知.现在想要通过随机选取n个人调查他们的态度,并由此来估计总体中赞同这个提议的人员比例p. 设Xi=1表 ...
- [svn]svn conflict 冲突解决
转自:http://www.gezila.com/tutorials/17290.html 目录: 1. 同一处修改文件冲突 1.1. 解决方式一 1.2. 解决方式二 1.3. 解决总结 2. 手动 ...