--DrWhICOqskFTAXiy Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable * Newbie (saleo / bellsouth.net) wrote: > I am a Ruby, OO newbie. I mostly work with databases. I am trying to use > ruby for scripting some of my routine tasks. > > I am trying to sub class a File, to create a HtmlReportFile with > additional methods. > > class HtmlReport < File > > def initialize(filename) > open(filename,'w') #Don't know why this does not return a file > object > end [snipped] Initialize doesn't return a value; it's called when the object is instantiated (created). So the return value from open is disappearing into thin air. The "correct" way to do this is to use "super" to pass the appropriate arguments to File#initialize, like so: # load CGI library (for CGI.escapeHTML) require 'cgi' class HtmlReport < File HEADER = '<html><head><title>%s</title></head><body>' FOOTER = '</body></html>' def initialize(path) # call File#initialize with the correct arguments super(path, 'w') end def header(title) # HTML-escape the report title self.puts HEADER % CGI.escapeHTML(title) end def content(text) self.puts text end def footer self.puts FOOTER close end end [snipped] > if $0 == __FILE__ > my_report = HtmlReport.new('test.html') > my_report.header('Test report') > my_report.content('Some text') > my_report.footer > end With the code I pasted above, running your test code produces a file called test.html that looks like so: <html><head><title>Test report</title></head><body> Some text </body></html> Hope that helps... -- Paul Duncan <pabs / pablotron.org> pabs in #ruby-lang (OPN IRC) http://www.pablotron.org/ OpenPGP Key ID: 0x82C29562 --DrWhICOqskFTAXiy Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (GNU/Linux) iD8DBQFF0hMRzdlT34LClWIRAknpAJ9bZ+7HLIg3FYRjPiJsgkDcJsWMTwCcD4oz R67yIFx8jCkEXbI22NzaFxoォア ュュュュュナホト ミヌミ モノヌホチヤユメナュュュュュ ュュトラノテマニヤチリュュ