Hello,
Suppose there’s a variable called ‘a’ with a value = 1.
Then I build a string “#{a} is really two”, which when printed gives
“1 is really two”.
How could I add a 1 to the a in that string to make it “2 is really
two”?
Is there some way I could do something like “#{a}.to_i + 1 is really
two”?
Best,
Mayuresh K. wrote:
Hello,
Suppose there’s a variable called ‘a’ with a value = 1.
Then I build a string “#{a} is really two”, which when printed gives
“1 is really two”.
How could I add a 1 to the a in that string to make it “2 is really
two”?
Is there some way I could do something like “#{a}.to_i + 1 is really
two”?
Best,
Inside #{} you can put whatever you want. “#{a+1} is really two”.
TPR.
2008/9/1 Thomas B. [email protected]:
two"?
Best,
Inside #{} you can put whatever you want. “#{a+1} is really two”.
And if you want to modify the string after evaluation you can do this:
irb(main):012:0> s = “1 is really two”
=> “1 is really two”
irb(main):013:0> s.sub!(/\d+/) {|m| m.to_i + 1}
=> “2 is really two”
irb(main):014:0> s
=> “2 is really two”
Or even
irb(main):005:0> s = “1 is really two”
=> “1 is really two”
irb(main):006:0> s[/\d+/] = ($&.to_i + 1).to_s
=> “2”
irb(main):007:0> s
=> “2 is really two”
Kind regards
robert