博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ruby 字符串学习笔记1
阅读量:4323 次
发布时间:2019-06-06

本文共 1225 字,大约阅读时间需要 4 分钟。

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 = <
"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"
<
"There once was a man from Peru\nWhose limericks stopped on line two\n"

 

转载于:https://www.cnblogs.com/or2-/p/5006902.html

你可能感兴趣的文章
poj2253 最短路 floyd Frogger
查看>>
springboot:session集中存储到redis
查看>>
《Python编程快速上手+让繁琐工作自动化》第12章实践项目:空行插入程序
查看>>
POJ 2986 A Triangle and a Circle(三角形和圆形求交)
查看>>
css3最新技术教程
查看>>
【tool】测试驱动开发全攻略
查看>>
VIM命令图---可定制版
查看>>
《坐热板凳》第八次团队作业:Alpha冲刺(第三天)
查看>>
关于wxWidgets
查看>>
codevs 1160 蛇形矩阵
查看>>
在outlook中查找Skype的聊天记录
查看>>
netsh命令
查看>>
nginx set变量后lua无法改值
查看>>
baseAdapter
查看>>
别让你妈知道!
查看>>
JAVA设计模式之迭代子模式
查看>>
Java程序生成exe可执行文件
查看>>
什么是blob,mysql blob大小配置介绍
查看>>
模运算的规则
查看>>
CSS样式布局入门介绍,非常详尽
查看>>