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的更多相关文章

  1. ruby字符串学习笔记5

    1获取字符串某部分 s = "My kingdom for a string!" s.slice(3,7) # kingdom s[3,7] # kingdom s[/.ing/] ...

  2. ruby字符串学习笔记4

    1 单独处理字符串的字符 如果处理的是ASCII码的文档使用string#each_byte 注意 没有 string#each方法,String#each_byte 速度比 String#scan快 ...

  3. ruby 字符串学习笔记3

    ascii转字符或者字符串转ascii "a".ord # => 97 "!".ord # => 33 "\n".ord # = ...

  4. Swift 2.0 字符串学习笔记(建议掌握OC字符串知识的翻阅)

    自己公司开现在使用OC语言在写,但Swift似乎是苹果更推荐使用的开发语言,估计也是未来开发的趋势,自己以前有接触swift,但又由于公司的项目赶,也没有时间去好好地学习这款开发语言.现在年底了,项目 ...

  5. ruby语言学习笔记2

    ruby学习笔记2  (摘自<ruby程序设计语言教程(中文版)>.pdf,全书25页) 1.同一个问题ruby有多个解决方案,途径 ruby之父:松本行弘(Matz),1993年创立 r ...

  6. ruby 字符串学习2

    在一个ruby字符串中包含表但是或者变量.想使用不同的值替换表达式或者变量 1 类似java 或者python的printf-style方式 template = 'Oceania has alway ...

  7. ruby编程语言-学习笔记5(第5章 语句和控制结构)

    以下是2种表达方式一样. if expression code end if expression then #推荐这种形式 code end expression的值不是false或nil,则cod ...

  8. ruby编程语言-学习笔记4(第4章 表达式和操作符)

    4.6.9 范围  Flip-Flops:  ..和... ..和... 操作符不是基于方法的,无法重定义.(优先级比较低) x+1 .. x*x   #可以认为是x+1 至 x*x 的范围 因为操作 ...

  9. ruby编程语言-学习笔记2(第4章 表达式和操作符)

    对属性和数组元素的赋值 o.m + = 1  ===>o.m = (o.m()+ 1)  # 对 o.m()这个方法的结果+1 对数组元素的赋值也是通过 方法调用来完成. 0.[] = 1 == ...

随机推荐

  1. ABBYY FineReader出现错误代码258

    ABBYY FineReader 12OCR文字识别软件能够快速方便地将扫描纸质文档.PDF文件和数码相机的图像转换成可编辑.可搜索的文本,包括Word.Excel.文本文档.PPT等格式,受到不少用 ...

  2. RequireJS 文件合并压缩

    RequireJS的define 以及require 对于我们进行简化JavaScript 开发,进行模块化的处理具有很大的帮助 但是请求加载的js 文件会有一些影响,一般的处理是对于文件进行压缩,但 ...

  3. 解决IE11出现异常SCRIPT5011:不能执行已释放Script的代码

    功能概述: 最近做了一个教育科研系统,由于时间比较紧,所以能集成的功能都尽量做到了一起,其中一个上传附件的功能,在基类控制器BaseController 中建了一个Action返回视图,其他需要上传附 ...

  4. Javascript 数组之判断取值和数组取值

    题目一:var arr = [ '100px', 'abc'-6, [], -98765, 34, -2, 0, '300', , function(){alert(1);}, null, docum ...

  5. 【Reporting Services 报表开发】— 数据表的使用

    一.打开 SQL Server Business Intelligence Development Studio,新建项目—>商业智能项目—> 报表服务器项目,命名为CH3 二.在报表文件 ...

  6. 查看CentOS版本方法

    查看内核版本 这个命令适用于所有的linux,包括Redhat.SuSE.Debian.Centos等发行版. root@MyMail ~ # uname Linux root@MyMail ~ # ...

  7. C语言每日一题之No.5

    总在想,但凡编程基础正常点,都不至于惨败到这个地步.也像大多数人毕业出来,新鲜的第一份工作,如果做得好还可以略有成就感,做得一般还有提升的空间,但至少不至于像我这样基本没基础的被鄙视得一塌糊涂,被外界 ...

  8. gcc/g++ 静态动态库 混链接.

    我的环境: centos6 x64. gcc4.4.7 在使用gcc/g++ 编译程序时我们希望指向一些库是使用静态的链接方式. 另外的一些是动态的方式. 我以boost 为例. 如果我们要使用静态库 ...

  9. WeX5和BeX5比较

    http://wex5.com/cn/wex5和bex5比较/ WeX5和BeX5比较 许多对WeX5和BeX5略有了解得人都知道,WeX5和BeX5是完全共用前端框架技术的.但是WeX5和BeX5是 ...

  10. Tomcat服务器搭建

    一.JDK环境搭建 二.tomcat下载安装 三.tomcat服务启动 cmd> net start  tomcat8 四.查看tomcat服务器启动情况: http://localhost:8 ...