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 == ...
随机推荐
- CorelDRAW 实现蒙版效果的方法
CorelDRAW能够实现很多意想不到的小效果,其中包括了位图图像软件的处理功能,蒙版效果就是其中的一项.作为矢量图形处理软件,从理论上讲它并不具备蒙板技术,然而只是我们平常没有用到而已,利用图框精确 ...
- P1010 笨小猴【tyvj】
/*=========================================================== P1010 笨小猴 描述 Description 笨小猴的词汇量很小,所以每 ...
- Oracle 11g 数据库自动备份执行脚本
@echo offsetlocal enabledelayedexpansiontitle %date% %time:~,8% by LiaoNing Sunray Software Technolo ...
- 导入安卓项目的时候,发生错误:Cause: peer not authenticated
导入安卓项目时出现Cause: peer not authenticated. 在网上搜了解决方案,都没有凑效.后来干脆插上手机直接debug安装,竟然成功了,成功了,成功了!!!! 然后再次buil ...
- css_三种引入方法
CSS是英文Cascading Style Sheets的缩写,称为层叠样式表,用于对页面进行美化. 详请:http://www.w3school.com.cn/h.asp 其存在方式有三种:元素内联 ...
- Linux 下增大tomcat内存
我的服务器的配置: # OS specific support. $var _must_ be set to either true or false. JAVA_OPTS="-Xms10 ...
- java将office文档pdf文档转换成swf文件在线预览
第一步,安装openoffice.org openoffice.org是一套sun的开源office办公套件,能在widows,linux,solaris等操作系统上执行. 主要模块有writer(文 ...
- 【jmete】Bean shell使用(一)
一.什么是Bean Shell BeanShell是一种完全符合Java语法规范的脚本语言,并且又拥有自己的一些语法和方法; BeanShell是一种松散类型的脚本语言(这点和JS类似); BeanS ...
- eclipse中tomcat发布失败(Could not delete May be locked by another process)原因及解决办法
在eclipse中tomcat发布项目时,偶尔出现了以下情况: publishing to tomcat v7.0 services at localhost has encountered a pr ...
- 剑指offer系列22--二叉树中和为某一值的路径
22[题目]输入一颗二叉树和一个整数,打印出二叉树中结点值的和为输入整数的所有路径. * 路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径 * [思路]只要从根结点到叶结点一条一条遍 ...