Beginner question

Discussion in 'Ruby' started by Mikael Larsson, Jul 4, 2004.

  1. Hi

    I wrote about Ruby a year ago, I find it intersting, so I bought "Hal
    Fulton's" book "the Ruby way", but I haven'thad time to read it until now.

    Tho book is really good, but the writer don't explain this construction (from
    page 81) (or I haven't found the explanation):

    class String
    def rot13
    self.tr!("A-Ma-mN-Zn-z", "N-Zn-zA-Ma-m")
    end
    end

    str = "Micke"

    puts "#{str} become #{str.rot13()}" #Micke become Zvpxr
    puts "#{str} become #{str.rot13()}" #Zvpxr become Micke
    puts "#{str} become #{str.swapcase()}" #Micke become mICKE

    For me looks like we are doing some implicit inherence here, like this:
    class String < String
    def rot13
    self.tr!("A-Ma-mN-Zn-z", "N-Zn-zA-Ma-m")
    end
    end

    Dose any one have a good explanation?

    Regards,
    Mike
    Mikael Larsson, Jul 4, 2004
    #1
    1. Advertising

  2. Mikael Larsson wrote:
    >
    > class String
    > def rot13
    > self.tr!("A-Ma-mN-Zn-z", "N-Zn-zA-Ma-m")
    > end
    > end
    >
    > str = "Micke"
    >
    > puts "#{str} become #{str.rot13()}" #Micke become Zvpxr
    > puts "#{str} become #{str.rot13()}" #Zvpxr become Micke
    > puts "#{str} become #{str.swapcase()}" #Micke become mICKE
    >
    > For me looks like we are doing some implicit inherence here, like this:
    > class String < String
    > def rot13
    > self.tr!("A-Ma-mN-Zn-z", "N-Zn-zA-Ma-m")
    > end
    > end
    >
    > Dose any one have a good explanation?


    What you're seeing is the fact that you can reopen a class and add new
    methods to it at any time. Your String class doesn't inherit from the
    original String class; it *is* the original String class. You're adding a
    new method to that class.

    Try this example for comparison:

    ###

    class Test
    def a
    123
    end
    end

    test = Test.new
    p test.a

    class Test
    def b
    a
    end
    end

    p test.a
    p test.b

    ###

    -Mike
    Michael Geary, Jul 4, 2004
    #2
    1. Advertising

  3. Mikael Larsson

    Hal Fulton Guest

    Mikael Larsson wrote:

    > Tho book is really good, but the writer don't explain this construction (from
    > page 81) (or I haven't found the explanation):
    >
    > class String
    > def rot13
    > self.tr!("A-Ma-mN-Zn-z", "N-Zn-zA-Ma-m")
    > end
    > end


    [snip]

    > For me looks like we are doing some implicit inherence here, like this:
    > class String < String
    > def rot13
    > self.tr!("A-Ma-mN-Zn-z", "N-Zn-zA-Ma-m")
    > end
    > end
    >
    > Dose any one have a good explanation?


    Hi, Mikael...

    I think you're failing to understand that Ruby's classes are "open" --
    i.e., all we're doing here is adding a new method to the String class.

    All instances of String have access to that method once you add it.
    (Even the ones that existed before the method was added.)

    Does that help any?


    Hal
    Hal Fulton, Jul 4, 2004
    #3
    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. Jerker Hammarberg

    Beginner question: What trigs processes

    Jerker Hammarberg, Jul 17, 2003, in forum: VHDL
    Replies:
    16
    Views:
    1,626
    Mike Treseler
    Jul 22, 2003
  2. smu
    Replies:
    3
    Views:
    2,635
    shobhit24
    Jun 28, 2006
  3. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    572
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  4. Rensjuh
    Replies:
    7
    Views:
    964
    Mabden
    Sep 2, 2004
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    213
    7stud --
    Apr 12, 2011
Loading...

Share This Page