# Substituting variables in a method

**URL:** <https://www.ruby-forum.com/t/substituting-variables-in-a-method/104829>\
**Category:** Ruby\
**Created:** [May 23, 2007, 1:50am UTC](https://www.ruby-forum.com/t/substituting-variables-in-a-method/104829 "2007-05-23T01:50:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_WSRyder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Michael\_WSRyder](https://www.ruby-forum.com/u/Michael_WSRyder)\
**Post date:** [May 23, 2007, 1:50am UTC](https://www.ruby-forum.com/t/substituting-variables-in-a-method/104829/1 "2007-05-23T01:50:47Z")

</div>

I am trying to figure out how to pass variables for the parameters to a  
method. I want to do something like b = a.tr({c}, {d}) where ‘c’ is the  
text to be matched and ‘d’ is the replacement text. Obviously the above  
does not work, nor does b = a.tr(#{c}, ${d}). Is this even possible or  
do I need to work out some regular expression to do the same thing.  
What I am trying to do is have a method where I can say change all  
periods to commas and commas to periods or change all \*s to #s or …

---

<div class="post-metadata">

**Author:** ![Michael\_WSRyder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Michael\_WSRyder](https://www.ruby-forum.com/u/Michael_WSRyder)\
**Post date:** [May 23, 2007, 2:26am UTC](https://www.ruby-forum.com/t/substituting-variables-in-a-method/104829/2 "2007-05-23T02:26:49Z")

</div>

On 5/23/07, Michael W. Ryder [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am trying to figure out how to pass variables for the parameters to a  
> method. I want to do something like b = a.tr({c}, {d}) where ‘c’ is the  
> text to be matched and ‘d’ is the replacement text. Obviously the above  
> does not work, nor does b = a.tr(#{c}, ${d}). Is this even possible or  
> do I need to work out some regular expression to do the same thing.  
> What I am trying to do is have a method where I can say change all  
> periods to commas and commas to periods or change all \*s to #s or …

I’m not sure if this is what you want, but…

str1 = "I am a string. "  
str2 = "You are not. "  
str3 = str1 + str2

str4 = str3.gsub(“#{str1}”,“#{str2}”)  
p str1  
p str2  
p str3  
p str4

Harry

–

A Look into Japanese Ruby List in English  
[http://www.kakueki.com/](http://www.kakueki.com/)

---

<div class="post-metadata">

**Author:** ![Michael\_WSRyder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Michael\_WSRyder](https://www.ruby-forum.com/u/Michael_WSRyder)\
**Post date:** [May 23, 2007, 3:10am UTC](https://www.ruby-forum.com/t/substituting-variables-in-a-method/104829/3 "2007-05-23T03:10:51Z")

</div>

Harry K. wrote:

> p str3  
> p str4
> 
> Harry

That’s what I was looking for. I would never have thought of enclosing  
the variables in double quotes. It also works for the .tr method.  
Is there some place that describes how to do these types of things or is  
it just a matter of experimenting or asking others?

---

<div class="post-metadata">

**Author:** ![Michael\_WSRyder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Michael\_WSRyder](https://www.ruby-forum.com/u/Michael_WSRyder)\
**Post date:** [May 23, 2007, 3:45am UTC](https://www.ruby-forum.com/t/substituting-variables-in-a-method/104829/4 "2007-05-23T03:45:54Z")

</div>

Rick DeNatale wrote:

> On 5/22/07, Harry K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > str4 = str3.gsub(“#{str1}”,“#{str2}”)
> 
> The string interpolation is completely unnecessary, this is the same as:  
> str4 = str3.gsub(str1, str2)

How does one know when to use string interpolation and when it isn’t  
needed? I know that if I want to put a variable in the middle of a  
string I have to use interpolation so I thought I also needed something  
like that in this case as I didn’t want to convert c’s to d’s.

---

<div class="post-metadata">

**Author:** ![Michael\_WSRyder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Michael\_WSRyder](https://www.ruby-forum.com/u/Michael_WSRyder)\
**Post date:** [May 23, 2007, 3:27am UTC](https://www.ruby-forum.com/t/substituting-variables-in-a-method/104829/5 "2007-05-23T03:27:52Z")

</div>

On 5/22/07, Harry K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> str4 = str3.gsub(“#{str1}”,“#{str2}”)

The string interpolation is completely unnecessary, this is the same as:  
str4 = str3.gsub(str1, str2)

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![Michael\_WSRyder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Michael\_WSRyder](https://www.ruby-forum.com/u/Michael_WSRyder)\
**Post date:** [May 23, 2007, 4:35am UTC](https://www.ruby-forum.com/t/substituting-variables-in-a-method/104829/6 "2007-05-23T04:35:58Z")

</div>

On 5/23/07, Michael W. Ryder [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> needed? I know that if I want to put a variable in the middle of a  
> string I have to use interpolation so I thought I also needed something  
> like that in this case as I didn’t want to convert c’s to d’s.

OK, I gave a bad example.  
Maybe this is a little better.

str1 = "str1 string. "  
str2 = “more stuff”  
str3 = str1.gsub(str1,str2)  
str4 = str1.gsub(“str1”,“str2”)  
str5 = str1.gsub(“str1”,“Now #{str2}”)

p str1  
p str2  
p str3  
p str4  
p str5

Harry

–

A Look into Japanese Ruby List in English  
[http://www.kakueki.com/](http://www.kakueki.com/)

---

<div class="post-metadata">

**Author:** ![Michael\_WSRyder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Michael\_WSRyder](https://www.ruby-forum.com/u/Michael_WSRyder)\
**Post date:** [May 23, 2007, 6:40am UTC](https://www.ruby-forum.com/t/substituting-variables-in-a-method/104829/7 "2007-05-23T06:40:07Z")

</div>

In message [pYM4i.345947$removed\_email\_address@domain.invalid](mailto:pYM4i.345947$removed_email_address@domain.invalid), “Michael W. Ryder”  
writes:

> How does one know when to use string interpolation and when it isn’t  
> needed? I know that if I want to put a variable in the middle of a  
> string I have to use interpolation so I thought I also needed something  
> like that in this case as I didn’t want to convert c’s to d’s.

It’s simple. If you are trying to embed a variable in a literal string,  
you  
need string interpolation. That’s it. That’s the only time you need  
it.

-s

---

<div class="post-metadata">

**Author:** ![Michael\_WSRyder](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/53a042/32.png) [@Michael\_WSRyder](https://www.ruby-forum.com/u/Michael_WSRyder)\
**Post date:** [May 23, 2007, 7:16am UTC](https://www.ruby-forum.com/t/substituting-variables-in-a-method/104829/8 "2007-05-23T07:16:08Z")

</div>

Harry K. wrote:

> > >
> 
> str1 = "str1 string. "
> 
> Harry

It took me a couple of minutes to figure out what these were doing and  
they do make some of it clearer. What you helped me to figure out was  
how to do something like:

a = “This is a test!”  
c = “!”  
d = “.”  
b = a.gsub(c, d)  
puts b

I didn’t want to convert c’s to d’s in this example, but wanted to  
convert the exclamation mark to a period and get ‘This is a test.’. I  
can also change c to “test” and d to “mess” to get ‘This is a mess!’.  
Thank you and Rick for the assistance.
