Redefining class

Discussion in 'Ruby' started by Henry Savr, Aug 24, 2006.

  1. Henry Savr

    Henry Savr Guest

    Thank you, guys, for help with my previous problems. With your
    assistance my second week with Ruby was more or less successful.

    However...
    I redefined couple methods of the Time class
    Everything worked fine, so I wanted to combine all the stuff in the
    module. But Ruby began complaining, when I just enclosed my job with
    words "module X1" and "end".

    That was fine:
    class Time
    ...
    end
    class Is
    ...
    end
    class Money
    ...
    end
    =========================
    Problems start here:
    module X1
    class Time
    ...
    end
    class Is
    ...
    end
    class Money
    ...
    end
    end
    ========================
    I think, that Ruby considers class Time inside the module as a new
    class, which belongs module X1. So I have to inform him/her :) , that
    it is the same class, as the original one. And I don't know how to do
    that.

    Please, help.
    Thank you
    Henry

    --
    Posted via http://www.ruby-forum.com/.
    Henry Savr, Aug 24, 2006
    #1
    1. Advertising

  2. On Fri, Aug 25, 2006 at 01:52:43AM +0900, Henry Savr wrote:
    > Problems start here:
    > module X1
    > class Time
    > ...
    > end
    > class Is
    > ...
    > end
    > class Money
    > ...
    > end
    > end
    > ========================
    > I think, that Ruby considers class Time inside the module as a new
    > class, which belongs module X1. So I have to inform him/her :) , that
    > it is the same class, as the original one. And I don't know how to do
    > that.


    class X; end
    module Y; class ::X; def foo; "X#foo"; end end end
    X.new.foo # => "X#foo"

    --
    Mauricio Fernandez - http://eigenclass.org - singular Ruby
    Mauricio Fernandez, Aug 24, 2006
    #2
    1. Advertising

  3. On 8/24/06, Mauricio Fernandez <> wrote:

    >
    > class X; end
    > module Y; class ::X; def foo; "X#foo"; end end end
    > X.new.foo # => "X#foo"


    Really? Was this under 1.8.x or 1.9, I'd be surprised if it was
    either, a gedanken experiment perhaps?

    irb(main):001:0> class X
    irb(main):002:1> end
    => nil
    irb(main):003:0> module Y; class X; def foo; "X#foo"; end; end
    irb(main):004:1> end
    => nil
    iirb(main):005:0> X.new.foo
    NoMethodError: undefined method `foo' for #<X:0xb7d4f560>
    from (irb):6
    from :0
    irb(main):006:0> Y::X.new.foo
    => "X#foo"

    The original poster's supposition is correct, modules (and classes for
    that matter) are namespaces,

    irb(main):001:0> module X1
    irb(main):002:1> class Time
    irb(main):003:2> end
    irb(main):004:1> end
    => nil
    irb(main):005:0> X1::Time == Time
    => false

    The Time class inside X1 is different than the Time class!

    irb(main):006:0> module X1
    irb(main):007:1> class Time
    irb(main):008:2> def foo
    irb(main):009:3> "foo"
    irb(main):010:3> end
    irb(main):011:2> end
    irb(main):012:1> end
    => nil
    irb(main):013:0> Time.new.foo
    NoMethodError: undefined method `foo' for Thu Aug 24 19:38:52 EDT 2006:Time
    from (irb):13
    from :0
    irb(main):014:0> X1::Time.new.foo
    => "foo"

    With it's own methods!

    And if we just refer to Time inside X1

    irb(main):023:0> module X1
    irb(main):024:1> def self.myTime
    irb(main):025:2> Time
    irb(main):026:2> end
    irb(main):027:1> end
    => nil
    irb(main):028:0> X1.myTime
    => X1::Time

    We get X1's Time.

    Now that begs the question of how to refer to the 'real' Time class
    inside a module. One way is with an "empty namespace."

    irb(main):015:0> module X1
    irb(main):016:1> def self.baseTime
    irb(main):017:2> ::Time
    irb(main):018:2> end
    irb(main):019:1> end
    => nil
    irb(main):020:0> X1.baseTime
    => Time
    irb(main):021:0> X1.baseTime == Time
    => true


    --
    Rick DeNatale

    My blog on Ruby
    http://talklikeaduck.denhaven2.com/
    Rick DeNatale, Aug 25, 2006
    #3
  4. On Fri, Aug 25, 2006 at 08:46:54AM +0900, Rick DeNatale wrote:
    > On 8/24/06, Mauricio Fernandez <> wrote:
    > >
    > >class X; end
    > >module Y; class ::X; def foo; "X#foo"; end end end

    ==
    This is what my message was about; you omitted it in your irb session.

    > >X.new.foo # => "X#foo"

    >
    > Really? Was this under 1.8.x or 1.9,


    Under

    RUBY_VERSION # => "1.8.5"
    RUBY_RELEASE_DATE # => "2006-07-07"

    > I'd be surprised if it was either, a gedanken experiment perhaps?


    No. I ran that code under xmpfilter
    http://eigenclass.org/hiki.rb?ruby xmpfilter 0.2.0

    It's pretty convenient for ruby-talk postings, and the output is much easier
    to read than irb transcripts.

    > irb(main):001:0> class X
    > irb(main):002:1> end
    > => nil
    > irb(main):003:0> module Y; class X; def foo; "X#foo"; end; end

    ========
    this is not the code I wrote (it's missing the ::)
    > irb(main):004:1> end
    > => nil
    > iirb(main):005:0> X.new.foo
    > NoMethodError: undefined method `foo' for #<X:0xb7d4f560>
    > from (irb):6
    > from :0
    > irb(main):006:0> Y::X.new.foo
    > => "X#foo"

    [...]

    With the original code:

    irb(main):001:0> class X; end
    => nil
    irb(main):002:0> module Y; class ::X; def foo; "X#foo"; end end end
    => nil
    irb(main):003:0> X.new.foo
    => "X#foo"

    (definitely harder to read :)

    --
    Mauricio Fernandez - http://eigenclass.org - singular Ruby
    Mauricio Fernandez, Aug 25, 2006
    #4
  5. On 8/24/06, Mauricio Fernandez <> wrote:
    > On Fri, Aug 25, 2006 at 08:46:54AM +0900, Rick DeNatale wrote:


    > > irb(main):003:0> module Y; class X; def foo; "X#foo"; end; end

    > ========
    > this is not the code I wrote (it's missing the ::)


    Okay, I missed that, I guess it's time for new eyeglasses.
    --
    Rick DeNatale

    My blog on Ruby
    http://talklikeaduck.denhaven2.com/
    Rick DeNatale, Aug 25, 2006
    #5
  6. Henry Savr

    Guest

    Hi --

    On Fri, 25 Aug 2006, Rick DeNatale wrote:

    > On 8/24/06, Mauricio Fernandez <> wrote:
    >> On Fri, Aug 25, 2006 at 08:46:54AM +0900, Rick DeNatale wrote:

    >
    >> > irb(main):003:0> module Y; class X; def foo; "X#foo"; end; end

    >> ========
    >> this is not the code I wrote (it's missing the ::)

    >
    > Okay, I missed that, I guess it's time for new eyeglasses.


    Or eigenglasses :)


    David

    --
    http://www.rubypowerandlight.com => Ruby/Rails training & consultancy
    ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <-----
    http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log
    http://www.manning.com/black => book, Ruby for Rails
    http://www.rubycentral.org => Ruby Central, Inc.
    , Aug 27, 2006
    #6
    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. Nick Bassiliades

    Redefining an enumerated attribute type

    Nick Bassiliades, Dec 9, 2005, in forum: XML
    Replies:
    1
    Views:
    437
    Henry S. Thompson
    Dec 12, 2005
  2. uli
    Replies:
    5
    Views:
    4,838
    Dietmar Kuehl
    Apr 21, 2004
  3. Xiangliang Meng
    Replies:
    1
    Views:
    1,592
    Victor Bazarov
    Jun 21, 2004
  4. syd
    Replies:
    2
    Views:
    261
    Greg Ewing
    Apr 8, 2005
  5. Belebele
    Replies:
    6
    Views:
    347
    Victor Bazarov
    Nov 2, 2007
Loading...

Share This Page