Forum: Ruby Same class, different module

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
9c7f2f44463d1d4890f73e8a8229dd29?d=identicon&s=25 Caleb Tennis (Guest)
on 2005-12-14 22:05
(Received via mailing list)
Here's a small snippet of code I'm trying to hack on:

module Mod1
  class A
    def meth
      puts "in Mod 1"
    end
  end

  class B
    def initialize
      A.new.meth
    end
  end
end

module Mod2
  class A < Mod1::A
    def meth
      put "in Mod 2"
    end
  end

  class B < Mod1::B
  end
end

p Mod1::B.new
p Mod2::B.new



caleb@tcdevel ~ $ ruby testit.rb
in Mod 1
#<Mod1::B:0xb7c888dc>
in Mod 1
#<Mod2::B:0xb7c88508>

What I'm trying to accomplish is having B reference A but in the same
module
its currently in.  I can accomplish this by redefining the initialize
method
again in Mod2::B, such that it looks exactly the same as Mod1::B, but
I'm
hoping to find a trick to avoid having to do that.

Any thoughts?

Caleb
Fe9b2d0628c0943af374b2fe5b320a82?d=identicon&s=25 Eero Saynatkari (rue)
on 2005-12-14 22:22
Caleb Tennis wrote:
> Here's a small snippet of code I'm trying to hack on:
>
> <snip due to ruby-forum.com quote limit />
>
> What I'm trying to accomplish is having B reference A but in the same
> module
> its currently in.  I can accomplish this by redefining the initialize
> method
> again in Mod2::B, such that it looks exactly the same as Mod1::B, but
> I'm
> hoping to find a trick to avoid having to do that.
>
> Any thoughts?

You could use #const_get, possibly, since a method call is dynamic.

> Caleb


E
Fe57662c550fb3cce44c398ddf2dd706?d=identicon&s=25 itsme213 (Guest)
on 2005-12-15 23:58
(Received via mailing list)
module M1
  class A; def foo; "foo"; end; end
end

module M2
  A = M1::A
  class A; def bar; "bar"; end; end
end

a = M2::A

a.foo #=> "foo"
a.bar #=> "bar"

Is that what you meant?

However, note that:

M2::A == M1::A

"Caleb Tennis" <caleb@aei-tech.com> wrote in message
news:200512141601.46013.caleb@aei-tech.com...
This topic is locked and can not be replied to.