From: Abder-rahman Ali on 12 Jul 2010 12:35 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. -- Posted via http://www.ruby-forum.com/.
From: Robert Klemme on 12 Jul 2010 12:53 On 12.07.2010 18:35, Abder-rahman Ali wrote: > 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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
From: Angus Hammond on 12 Jul 2010 12:53 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/10 17:35, Abder-rahman Ali wrote: > 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. >
From: Abder-rahman Ali on 12 Jul 2010 15:10 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? -- Posted via http://www.ruby-forum.com/.
From: Abder-rahman Ali on 12 Jul 2010 15:27 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/.
|
Next
|
Last
Pages: 1 2 Prev: Adding cookies to my http client Next: [UPDATE] Try Ruby is back online. |