Ruby 字符串连接问题 [英] Problem with Ruby string concatenation
本文介绍了Ruby 字符串连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这有效
irb(main):001:0> name = "Rohit " "Sharma"
=> "Rohit Sharma"
但这没有
irb(main):001:0> fname = "Rohit "
=> "Rohit "
irb(main):002:0> lname = "Sharma"
=> "Sharma"
irb(main):003:0> name = fname lname
它给出了这个错误
NoMethodError: undefined method `fname' for main:Object
from (irb):3
请提供一些建议.提前致谢.
Please provide some suggestions. Thanks in advance.
更新
得到答案后,我写了一篇博客文章.请检查一下.
After getting the answers I have written a blog post. Please check it out.
推荐答案
该错误与 fname
必须是一个函数才能工作有关.相反,尝试
The error is related to the fact that fname
would have to be a function for this to work. Instead, try
name = fname + lname
甚至
name = "#{fname}#{lname}"
但是你有过的地方
name = "Rohit " "Sharma"
这是一个特例,因为 Ruby 会自动连接两个字符串.
it is a special case, since Ruby will join the two strings automatically.
这篇关于Ruby 字符串连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文