Problem extending (adding methods) to class

N

Nit Khair

David said:
Why not use a module?

module MyForm
attr_accessor :myfields
def get_fields
@myfields
end
end

class Ncurses::Form::FORM
include MyForm
end
Interesting. But sorry if I am missing something. as I understand it, i
can't subclass a module, nor class Ncurses::Form::FORM. So i wont be
able to subclass this, right ?
 
D

David A. Black

Hi --

Interesting. But sorry if I am missing something. as I understand it, i
can't subclass a module, nor class Ncurses::Form::FORM. So i wont be
able to subclass this, right ?

It would still have the same 'new' issue. I'm suggesting, though, that
modules might be a good alternative to subclassing.


David

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 
N

Nit Khair

It would still have the same 'new' issue. I'm suggesting, though, that
modules might be a good alternative to subclassing.


David

But modules can't maintain state, right.

While refactoring, i began dumping a lot of methods in a Module.

Then i needed differential behavior based on "kind_of", e.g. a basic
key_handler, a key_handler for tabular forms etc. The cleanest way that
i could think of was having the form objects having their own key
handling.
So i needed to differentiate forms based on behaviour. Thus I have a
heirarchy of forms and applications. The appropriate handler is called
based on what kind of form was created.

I am really new here, and struggling with concepts, but i understand
that yuo can have only one parent/super class, so modules help out in
acquiring more behavior (sort of multiple inheritance).
I'm suggesting, though, that
modules might be a good alternative to subclassing.
However, in the subclassing I am doing, the objects my application
consists of (composition) are descending from their own super-classes.
Yes, they still call "static" methods of the original module i created
(i mean convenience methods like printing errors, statuses etc).

This is why i was asking (on another thread, too) of actual projects
whose source one can learn these things from ( as against books,
samples, examples etc of which i have read enough - but will keep
reading!).

Perhaps the ruby source itself will be good quality - i really need to
see how real-life medium size projects in ruby are organized, classed
etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,173
Latest member
GeraldReund
Top