【发布时间】:2013-12-21 07:05:02
【问题描述】:
我有一个包含字符串的变量,在运行时我要替换一些存储在该字符串中的变量。
例如..
my_string = "Congrats you have joined groupName."
groupName = "*Name of my group*"
puts my_string
输出:-
"Congrats you have joined *name of the group*"
问题是:
my_string = " Congrats you have joined #{groupName}" expects groupName already exists.. but in my case i have to define my_string before variable in it.
解决方案 1:
一种方法可以是.. 像使用 gsub 一样替换字符串.. 但那不是一个好方法..
PS:
我正在努力实现的目标。我们有一组 100 条消息需要传递。我想在一个地方定义,并在需要时替换一些变量。现在我想在 application_controller 中定义所有这些变量(100 个),这样我就可以连接每个定义的变量(100 个)。并自动变量(在存储在这 100 个变量之一中的字符串中定义的变量)。 这种语言很混乱..检查我上面解释的例子..
【问题讨论】:
-
my_string = " Congrats you have joined #{groupName}" expects groupName already exists.. but in my case i have to define my_string before variable in it.不是有效的 Ruby 代码。它也不是有效的英语。