Ruby编程之字符串拼接

临风暖阳 发表于 2022/11/01 07:42:54 2022/11/01
【摘要】 笔者主要用Ruby编写字符串拼接小案例
s1 = "This " << "is" << " a string " << 36 # char 36 is '$'
s2 = "This " + "is" + " a string " + 36.to_s
s3 = "This " "is" " a string " + 36.to_s
puts("(s1):" << s1)
puts("(s2):" << s2)
puts("(s3):" << s3)

(s1):This is a string $
(s2):This is a string 36
(s3):This is a string 36

s4 = "This " , "is" , " not a string!", 10
print("print (s4):" , s4, "\n")
x = "This " , "is" , " not a string!", 36
print("print (x):" , x, "\n")
puts("puts(x):", x)
puts("puts x.class is: " << (x.class).to_s )
print("print(x):" , x, "\n")
puts("puts(x):", x)
puts("puts x.class is: " << (x.class).to_s )

print (s4):["This ", "is", " not a string!", 10]
print (x):["This ", "is", " not a string!", 36]
puts(x):
This
is
 not a string!
36
puts x.class is: Array
print(x):["This ", "is", " not a string!", 36]
puts(x):
This
is
 not a string!
36
puts x.class is: Array

开发环境:Rubymine

The book of Ruby----A hands-on guide for the Adventurous---[美]Huw Collingbourne---no starch press

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区),文章链接,文章作者等基本信息,否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件至:cloudbbs@huaweicloud.com进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容。
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。