Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Extending class from other module

Reply
Thread Tools

Extending class from other module

 
 
Alexander Rysenko
Guest
Posts: n/a
 
      08-29-2006
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-forum.com/.

 
Reply With Quote
 
 
 
 
Eero Saynatkari
Guest
Posts: n/a
 
      08-29-2006
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-forum.com/.

 
Reply With Quote
 
 
 
 
Noah Easterly
Guest
Posts: n/a
 
      08-29-2006
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-forum.com/.


 
Reply With Quote
 
Jan Svitok
Guest
Posts: n/a
 
      08-29-2006
On 8/29/06, 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?


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/te...doc/index.html
[2] http://api.rubyonrails.com/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
cast to sub-class or extending instance of super or a.n.other Richard Maher Java 42 04-20-2009 03:19 PM
Decralation of class inside other class and definition outside this class =?ISO-8859-2?Q?Miros=B3aw?= Makowiecki C++ 2 07-12-2007 11:52 PM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Extending class to be a base class *properly* Matthias Kaeppler C++ 1 04-22-2005 06:05 AM
maps and class types: extending a class factory Simon Elliott C++ 0 01-11-2005 01:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57