Substitution

Discussion in 'Ruby' started by Abder-rahman Ali, Jul 12, 2010.

  1. 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/.
    Abder-rahman Ali, Jul 12, 2010
    #1
    1. Advertising

  2. 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.
    >
    Angus Hammond, Jul 12, 2010
    #2
    1. Advertising

  3. 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/
    Robert Klemme, Jul 12, 2010
    #3
  4. 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/.
    Abder-rahman Ali, Jul 12, 2010
    #4
  5. 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/.
    Abder-rahman Ali, Jul 12, 2010
    #5
  6. Abder-rahman Ali

    Ammar Ali Guest

    [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 <
    > 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/.
    >
    >
    Ammar Ali, Jul 12, 2010
    #6
  7. 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/.
    Abder-rahman Ali, Jul 12, 2010
    #7
  8. Abder-rahman Ali

    Ammar Ali Guest

    [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 <
    > 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
    Ammar Ali, Jul 12, 2010
    #8
  9. Abder-rahman Ali, Jul 12, 2010
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. valentin tihomirov

    Should this substitution be compilable?

    valentin tihomirov, Nov 28, 2004, in forum: VHDL
    Replies:
    12
    Views:
    791
    valentin tihomirov
    Nov 30, 2004
  2. Troll
    Replies:
    6
    Views:
    2,436
    Kris Wempa
    Sep 26, 2003
  3. Justin

    adobe multiline substitution

    Justin, Dec 8, 2003, in forum: Perl
    Replies:
    0
    Views:
    500
    Justin
    Dec 8, 2003
  4. Ashok

    Substitution Problem

    Ashok, Jul 18, 2004, in forum: Perl
    Replies:
    1
    Views:
    653
    Gunnar Hjalmarsson
    Jul 18, 2004
  5. Ian
    Replies:
    4
    Views:
    2,307
    Ben Bacarisse
    Feb 2, 2006
Loading...

Share This Page