[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Extending class from other module

Alexander Rysenko

8/29/2006 9:12:00 AM

1st question: If I got it right, modules in Ruby can be used as
namespaces. Is it ok? Is there some other (preffered) way?

2nd question: I need to extend some class from one module in another
one. Basically I do something like this:

module Mod1
class A1
end

class B2 < Mod2::B1
end
end

module Mod2
class B1
end
end

But this doesn't really work (seems like B1 isn't really found). How can
I do such a thing?

--
Posted via http://www.ruby-....

3 Answers

e

8/29/2006 2:48:00 PM

0

Alexander Rysenko wrote:
> 1st question: If I got it right, modules in Ruby can be used as
> namespaces. Is it ok? Is there some other (preffered) way?

That is the way to do it.

> 2nd question: I need to extend some class from one module in another
> one. Basically I do something like this:
>
> module Mod1
> class A1
> end
>
> class B2 < Mod2::B1
> end
> end
>
> module Mod2
> class B1
> end
> end
>
> But this doesn't really work (seems like B1 isn't really found). How can
> I do such a thing?

You need to flip those around. You cannot inherit from
Mod2::B1 before it is defined.

--
Posted via http://www.ruby-....

noah.easterly@gmail.com

8/29/2006 2:53:00 PM

0

module Mod1
class A1
end
end
module Mod2
class B1
end
end
class Mod1::B2 < Mod2::B1
end

Alexander Rysenko wrote:
> 1st question: If I got it right, modules in Ruby can be used as
> namespaces. Is it ok? Is there some other (preffered) way?
>
> 2nd question: I need to extend some class from one module in another
> one. Basically I do something like this:
>
> module Mod1
> class A1
> end
>
> class B2 < Mod2::B1
> end
> end
>
> module Mod2
> class B1
> end
> end
>
> But this doesn't really work (seems like B1 isn't really found). How can
> I do such a thing?
>
> --
> Posted via http://www.ruby-....

Jano Svitok

8/29/2006 2:59:00 PM

0

On 8/29/06, Alexander Rysenko <rysenko@gmail.com> wrote:
> 1st question: If I got it right, modules in Ruby can be used as
> namespaces. Is it ok? Is there some other (preffered) way?

Exactly. Look for example at test/unit[1], or rails[2] as they contain
a lot of classes divided into modules for namespace separation.

> 2nd question: I need to extend some class from one module in another
> one. Basically I do something like this:
>
> module Mod1
> class A1
> end
>
> class B2 < Mod2::B1
> end
> end
>
> module Mod2
> class B1
> end
> end
>
> But this doesn't really work (seems like B1 isn't really found). How can
> I do such a thing?

The problem is in the order of evaluation. Install rcov (gem install
rcov) and see for yourself what lines ruby has read already and what
not.

I guess the problem is that B1 is not known to ruby yet. You can fix
that by reversing the order, or just declaring the class without any
content:

module Mod2
class B1
end
end

module Mod1
class B2 < Mod2::B1
def xxx...
...
end
end
end

module Mod2
class B1
def yyy
...
end
end
end

I don't have ruby installed now, so I can't verify these things, so
please take it as such.

[1] http://ruby-doc.org/stdlib/libdoc/test/unit/rdoc/...
[2] http://api.rubyon...