From: "David A. Black" Date: 2008-10-03T03:17:30+09:00 Subject: Re: Problem extending (adding methods) to class Hi -- On Fri, 3 Oct 2008, Nit Khair wrote: > I am trying to extend a class developed by someone else (its from the > ncurses-ruby lib). I need to add some fields and methods to it. > > 1. I tried directly extending it the usual way. > class MyForm < FORM > > However, all attempts to access my methods would give a NoMethodError > on the base class Ncurses::FORM. Methods you add to a subclass are not visible to instances of the original class. > 2. I then looked up the source and copied the following *on top* of my > class MyForm. > > module Form > class FORM > attr_reader :user_object > > def user_object=(obj) > @user_object = obj > end > end > end > > I changed user_object to "my_fields". This approach works, but means i > cannot actually extend the class. I need a heirarchy below this each > adding its methods and data. > > Now I am wondering whether my approach is wrong. Is the class > locked/final in some way? No. If you want to add methods to it, you can just open it up and add them: require 'rubygems' require 'ncurses' class Ncurses::Form::FORM def my_method puts "Hello" end end n = Ncurses::Form::FORM.new n.my_method # Hello 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!