Substitution

A

Abder-rahman Ali

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

Angus Hammond

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

Robert Klemme

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?

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

Kind regards

robert
 
A

Abder-rahman Ali

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

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?
 
A

Abder-rahman Ali

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

Ammar Ali

[Note: parts of this message were removed to make it a legal post.]

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

Abder-rahman Ali

Ammar said:
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?
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?
 
A

Ammar Ali

[Note: parts of this message were removed to make it a legal post.]

On Mon, Jul 12, 2010 at 11:30 PM, Abder-rahman Ali <
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


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top