I have a variable containing a string and in run time i was to replace some variables which are stored in that string.
我有一个包含字符串的变量,在运行时我要替换存储在该字符串中的一些变量。
for example..
例如..
my_string = "Congrats you have joined groupName."
groupName = "*Name of my group*"
puts my_string
Output:-
输出: -
"Congrats you have joined *name of the group*"
issue is:
问题是:
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.
Solution 1:
解决方案1:
One way can be.. string replacment like using gsub.. but thats not good one..
一种方法可以..字符串替换,如使用gsub ..但这不是一个好...
PS:
PS:
What I am trying to achieve. We have some set of 100 messages that we have to deliver. I want to define at one single place and just replace some variable when needed. Now i want to define all these variables(100 ones) in the application_controller, so that I can just concatenate each variable(one of 100) defined. And automatically variable(variable which is defined in the string stored in one of those 100 variables). This language is quite confusing.. Check the example i explained above..
我想要实现的目标。我们必须提供一些100条消息。我想在一个地方定义,只需在需要时替换一些变量。现在我想在application_controller中定义所有这些变量(100个),这样我就可以连接定义的每个变量(100个中的一个)。并自动变量(在存储在这100个变量之一中的字符串中定义的变量)。这种语言很混乱..检查我上面解释的例子..
5 个解决方案
#1
11
You could store a format string:
您可以存储格式字符串:
my_string = "Congrats you have joined %s"
group_name = "My Group"
puts my_string % group_name # prints: Congrats you have joined My Group
For multiple variables in the same string you can use
对于同一个字符串中的多个变量,您可以使用
my_string = "Congrats you have joined %s %s"
group_name = ['group1', 'group2']
puts my_string % ['group1', 'group2']
will result:--
将导致: -
"Congrats you have joined group1 group2"
#2
22
Or you can do this:
或者你可以这样做:
2.0.0-p247 :034 > a = "I love my live, says %{who}"
=> "I love my live, says %{who}"
2.0.0-p247 :035 > a % { :who => "me" }
=> "I love my live, says me"
#3
3
You can use the I18n functionality to replace variables:
您可以使用I18n功能替换变量:
I18n.backend.store_translations :en,
:congrats => 'Congrats you have joined %{group_name}!'
I18n.translate :congrats, :group_name => 'My Group'
# => 'Congrats you have joined My Group!'
This way you only have a single point to maintain your texts. Your application_controller
is not the best place for static texts.
这样,您只需要一个点来维护您的文本。您的application_controller不是静态文本的最佳位置。
#4
0
my_string = "Congrats you have joined groupName."
groupName = "*Name of my group*"
puts my_string.gsub('groupName',groupName)
Output :
输出:
"Congrats you have joined *name of the group*"
What it does is search for the 'groupName' string and replace it with the content of the groupName variable
它的作用是搜索'groupName'字符串并将其替换为groupName变量的内容
#5
0
You can use eval to replace variables in runtime :
您可以使用eval在运行时替换变量:
my_string = 'Congrats you have joined #{groupName}.'
groupName = "*Name of my group*"
puts eval('"'+ my_string +'"')
#1
11
You could store a format string:
您可以存储格式字符串:
my_string = "Congrats you have joined %s"
group_name = "My Group"
puts my_string % group_name # prints: Congrats you have joined My Group
For multiple variables in the same string you can use
对于同一个字符串中的多个变量,您可以使用
my_string = "Congrats you have joined %s %s"
group_name = ['group1', 'group2']
puts my_string % ['group1', 'group2']
will result:--
将导致: -
"Congrats you have joined group1 group2"
#2
22
Or you can do this:
或者你可以这样做:
2.0.0-p247 :034 > a = "I love my live, says %{who}"
=> "I love my live, says %{who}"
2.0.0-p247 :035 > a % { :who => "me" }
=> "I love my live, says me"
#3
3
You can use the I18n functionality to replace variables:
您可以使用I18n功能替换变量:
I18n.backend.store_translations :en,
:congrats => 'Congrats you have joined %{group_name}!'
I18n.translate :congrats, :group_name => 'My Group'
# => 'Congrats you have joined My Group!'
This way you only have a single point to maintain your texts. Your application_controller
is not the best place for static texts.
这样,您只需要一个点来维护您的文本。您的application_controller不是静态文本的最佳位置。
#4
0
my_string = "Congrats you have joined groupName."
groupName = "*Name of my group*"
puts my_string.gsub('groupName',groupName)
Output :
输出:
"Congrats you have joined *name of the group*"
What it does is search for the 'groupName' string and replace it with the content of the groupName variable
它的作用是搜索'groupName'字符串并将其替换为groupName变量的内容
#5
0
You can use eval to replace variables in runtime :
您可以使用eval在运行时替换变量:
my_string = 'Congrats you have joined #{groupName}.'
groupName = "*Name of my group*"
puts eval('"'+ my_string +'"')