From: Ammar Ali on 12 Jul 2010 15:36 [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 < abder.rahman.ali(a)gmail.com> wrote: > 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. > -- > Posted via http://www.ruby-forum.com/. > >
From: Abder-rahman Ali on 12 Jul 2010 16:30 Ammar Ali 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? -- Posted via http://www.ruby-forum.com/.
From: Ammar Ali on 12 Jul 2010 17:23 [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 < abder.rahman.ali(a)gmail.com> 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
From: Abder-rahman Ali on 12 Jul 2010 17:31 Jazak Allah Khayr Ammar. I'll try it out Insha' Allah. -- Posted via http://www.ruby-forum.com/.
First
|
Prev
|
Pages: 1 2 Prev: Adding cookies to my http client Next: [UPDATE] Try Ruby is back online. |