# Substituting variables in a method

**URL:** <https://rubytalk.org/t/substituting-variables-in-a-method/37776>\
**Category:** ruby-talk\
**Created:** [22 May 2007 23:50 UTC](https://rubytalk.org/t/substituting-variables-in-a-method/37776 "2007-05-22T23:50:09Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_W\_Ryder1](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Michael\_W\_Ryder1](https://rubytalk.org/u/Michael_W_Ryder1)\
**Post date:** [22 May 2007 23:50 UTC](https://rubytalk.org/t/substituting-variables-in-a-method/37776/1 "2007-05-22T23:50:09Z")

</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](http://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](http://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:** ![Harry3](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@Harry3](https://rubytalk.org/u/Harry3)\
**Post date:** [23 May 2007 00:26 UTC](https://rubytalk.org/t/substituting-variables-in-a-method/37776/2 "2007-05-23T00:26:24Z")

</div>

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

> **···**
>
> On 5/23/07, Michael W. Ryder \<\_mwryder@worldnet.att.net\> 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 ...
> 
> --
> 
> A Look into Japanese Ruby List in English
> 
> > **[Kakueki.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=kakueki.com)**
> >
> > Start using this domain right away. Straightforward domain shopping experience. Quick access to your domain.

---

<div class="post-metadata">

**Author:** ![Michael\_W\_Ryder1](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Michael\_W\_Ryder1](https://rubytalk.org/u/Michael_W_Ryder1)\
**Post date:** [23 May 2007 01:10 UTC](https://rubytalk.org/t/substituting-variables-in-a-method/37776/3 "2007-05-23T01:10:03Z")

</div>

Harry Kakueki wrote:

> **···**
>
> > On 5/23/07, Michael W. Ryder \<\_mwryder@worldnet.att.net\> 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
> 
> 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:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [23 May 2007 01:26 UTC](https://rubytalk.org/t/substituting-variables-in-a-method/37776/4 "2007-05-23T01:26:44Z")

</div>

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

> **···**
>
> On 5/22/07, Harry Kakueki \<list.push@gmail.com\> wrote:
> 
> > 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\_W\_Ryder1](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Michael\_W\_Ryder1](https://rubytalk.org/u/Michael_W_Ryder1)\
**Post date:** [23 May 2007 01:45 UTC](https://rubytalk.org/t/substituting-variables-in-a-method/37776/5 "2007-05-23T01:45:03Z")

</div>

Rick DeNatale wrote:

> **···**
>
> > On 5/22/07, Harry Kakueki \<list.push@gmail.com\> 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:** ![Harry3](https://avatars.discourse-cdn.com/v4/letter/h/54ee81/32.png) [@Harry3](https://rubytalk.org/u/Harry3)\
**Post date:** [23 May 2007 02:35 UTC](https://rubytalk.org/t/substituting-variables-in-a-method/37776/6 "2007-05-23T02:35:20Z")

</div>

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

> **···**
>
> On 5/23/07, Michael W. Ryder \<\_mwryder@worldnet.att.net\> wrote:
> 
> > Rick DeNatale wrote:  
> > \> On 5/22/07, Harry Kakueki \<list.push@gmail.com\> 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.
> 
> --
> 
> A Look into Japanese Ruby List in English
> 
> > **[Kakueki.com is for sale | HugeDomains](https://www.hugedomains.com/domain_profile.cfm?d=kakueki.com)**
> >
> > Start using this domain right away. Straightforward domain shopping experience. Quick access to your domain.

---

<div class="post-metadata">

**Author:** ![Seebs](https://avatars.discourse-cdn.com/v4/letter/s/439d5e/32.png) [@Seebs](https://rubytalk.org/u/Seebs)\
**Post date:** [23 May 2007 04:39 UTC](https://rubytalk.org/t/substituting-variables-in-a-method/37776/7 "2007-05-23T04:39:12Z")

</div>

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

> **···**
>
> In message \<pYM4i.345947$g24.243267@newsfe12.phx\>, "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.

---

<div class="post-metadata">

**Author:** ![Michael\_W\_Ryder1](https://avatars.discourse-cdn.com/v4/letter/m/8e7dd6/32.png) [@Michael\_W\_Ryder1](https://rubytalk.org/u/Michael_W_Ryder1)\
**Post date:** [23 May 2007 05:15 UTC](https://rubytalk.org/t/substituting-variables-in-a-method/37776/8 "2007-05-23T05:15:10Z")

</div>

Harry Kakueki wrote:

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

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.

> **···**
>
> > On 5/23/07, Michael W. Ryder \<\_mwryder@worldnet.att.net\> wrote:
> > 
> > > \> On 5/22/07, Harry Kakueki \<list.push@gmail.com\> wrote:
