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.
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.
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
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?
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.
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?
On Mon, Jul 12, 2010 at 11:30 PM, Abder-rahman Ali <
[email protected]> 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
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.
I’ll try it out Insha’ Allah.