# Substitution

**URL:** <https://www.ruby-forum.com/t/substitution/191438>\
**Category:** Ruby\
**Created:** [July 12, 2010, 6:35pm UTC](https://www.ruby-forum.com/t/substitution/191438 "2010-07-12T18:35:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 12, 2010, 6:35pm UTC](https://www.ruby-forum.com/t/substitution/191438/1 "2010-07-12T18:35:32Z")

</div>

I have this part of code from “Why’s poignant guide to Ruby” that is  
intended to make substitution:

Note: code\_words here is a hash

…  
idea = gets  
code\_words.each do |real, code|  
idea.gsub!(real, code)  
end  
…

I know that “real” is what to find, and “code” is what to put in place.  
But, what I’m not getting here is why is this written:

idea.gsub!(…

I don’t mean the gsub! method, but, why “idea”? What does it here? How  
can we read that line of script?

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 12, 2010, 6:55pm UTC](https://www.ruby-forum.com/t/substitution/191438/2 "2010-07-12T18:55:14Z")

</div>

What that says is use the gsub! method on the object pointed to by idea  
(which is a string). Then in order to tell gsub! how to work we pass it  
some parameters which are real (the thing to look for) and code (the  
thing to replace it with).

The part you seem to be having trouble with is the “dot syntax”. The dot  
says run the method after the dot on the object returned by the thing  
before the dot.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 12, 2010, 6:56pm UTC](https://www.ruby-forum.com/t/substitution/191438/3 "2010-07-12T18:56:24Z")

</div>

On 12.07.2010 18:35, Abder-rahman Ali wrote:

> …
> 
> I know that “real” is what to find, and “code” is what to put in place.  
> But, what I’m not getting here is why is this written:
> 
> idea.gsub!(…
> 
> I don’t mean the gsub! method, but, why “idea”? What does it here? How  
> can we read that line of script?

This is simply a string read from stdin - nothing more.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 12, 2010, 9:10pm UTC](https://www.ruby-forum.com/t/substitution/191438/4 "2010-07-12T21:10:37Z")

</div>

Let me give an example I’m making similar to that, where I would like  
for example to substitute the “name” key’s value with a different name.  
What should I do.

Here is the script so far:  
[http://pastie.org/private/msbisytoddievgvohrrpq](http://pastie.org/private/msbisytoddievgvohrrpq)

So, yes. For example name =\> ‘Abder-Rahman’

My intention from this script is to replace the “name” with another  
value, but seems yet not working. What should I do?

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 12, 2010, 9:27pm UTC](https://www.ruby-forum.com/t/substitution/191438/5 "2010-07-12T21:27:11Z")

</div>

I think my MAIN point is this.

key.gsub!(toreplace, inplace)

key —\> It is what I insert.

Say key = Name

Now, for the “toreplace” and “inplace” parts, shouldn’t I enter values  
for them for the substition to work?

For example:

key.gsub!(‘Name’, ‘ID’)

Shouldn’t this replace ‘Name’ with ‘ID’.

But, in the example I saw in the book and the other that I mimicked, I  
find only the variable names but no values.

What should I do to complement the examples?

Thanks.

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 12, 2010, 10:30pm UTC](https://www.ruby-forum.com/t/substitution/191438/6 "2010-07-12T22:30:44Z")

</div>

Ammar A. wrote:

> Maybe the loop and gsub are causing the confusion. If all you want to do  
> is  
> replace the value associated with the key, you can simple assign it:
> 
> some\_hash[‘Name’] = “New Name”
> 
> Is that what you are trying to do?
> 
> On Mon, Jul 12, 2010 at 10:27 PM, Abder-rahman Ali \<

Jazak Allah Khayr Ammar for your reply.

Maybe your solution is a thing I was thinking of.

But, can you kindly tell me how the gsub! method is working on the  
example?

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 12, 2010, 11:23pm UTC](https://www.ruby-forum.com/t/substitution/191438/7 "2010-07-12T23:23:59Z")

</div>

On Mon, Jul 12, 2010 at 11:30 PM, Abder-rahman Ali \<  
removed\_email\_address@domain.invalid\> wrote:

> Maybe your solution is a thing I was thinking of.

It not a solution really, just the basic syntax.

But, can you kindly tell me how the gsub! method is working on the

> example?

The gsub! does not replace values in the hash, it is operating on the  
string  
that is returned by gets. Consider the following:

str = “Some short string”  
puts str # prints “Some short string”

str.gsub!(‘short’, ‘small’)  
puts str # prints “Some small string”

Adding some print statements can help you see what is going on. Try  
running  
the following:

info = {  
‘Name’ =\> ‘Joe’,  
‘Age’ =\> ‘20’  
}

template = “Name is Age years old”  
puts “template = #{template}”

info.each do |key, value|  
puts “replacing #{key} with #{value}”  
template.gsub!(key, value)  
puts “template = #{template}”  
end

Hope that helps,  
Ammar

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 12, 2010, 9:36pm UTC](https://www.ruby-forum.com/t/substitution/191438/8 "2010-07-12T21:36:16Z")

</div>

Maybe the loop and gsub are causing the confusion. If all you want to do  
is  
replace the value associated with the key, you can simple assign it:

some\_hash[‘Name’] = “New Name”

Is that what you are trying to do?

On Mon, Jul 12, 2010 at 10:27 PM, Abder-rahman Ali \<

---

<div class="post-metadata">

**Author:** ![swengineer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/97f17d/32.png) [@swengineer](https://www.ruby-forum.com/u/swengineer)\
**Post date:** [July 12, 2010, 11:31pm UTC](https://www.ruby-forum.com/t/substitution/191438/9 "2010-07-12T23:31:23Z")

</div>

Jazak Allah Khayr Ammar.

I’ll try it out Insha’ Allah.
