13

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:

One way can be.. string replacment like using gsub.. but thats not good one..

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..

Mohit Jain
  • 43,139
  • 57
  • 169
  • 274
  • 2
    Duplicate of http://stackoverflow.com/questions/554666/ruby-merging-variables-in-to-a-string – Yossi Jul 21 '11 at 12:44
  • `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.` isn't valid Ruby code. It also isn't valid English. – Andrew Grimm Sep 15 '11 at 00:04

5 Answers5

30

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" 
Nabheet
  • 1,285
  • 1
  • 12
  • 22
  • 2
    This is why Ruby is so cool. If anyone is curious, this is documented in the [Kernel#sprintf](http://ruby-doc.org/core-2.2.0/Kernel.html#method-i-sprintf) documentation (last example). – DannyB May 17 '15 at 08:07
16

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" 
Mohit Jain
  • 43,139
  • 57
  • 169
  • 274
Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
3

You can use the I18n functionality to replace variables:

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.

arnep
  • 5,971
  • 3
  • 35
  • 51
-1
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

Yoann Le Touche
  • 1,280
  • 9
  • 13
-1

You can use eval to replace variables in runtime :

my_string = 'Congrats you have joined #{groupName}.'
groupName = "*Name of my group*"
puts eval('"'+ my_string +'"')
romanlv
  • 1,432
  • 15
  • 15
  • check my question. I specified whats the issue with this format. – Mohit Jain Sep 14 '11 at 22:23
  • 1
    Mohit, pay attention to single quotes in my_string "'", this expression won't be evaluated until you use eval, so you can define string before defining variable.. – romanlv Oct 20 '11 at 14:07