# Ruby, OO newb, subclassing question

**URL:** <https://www.ruby-forum.com/t/ruby-oo-newb-subclassing-question/85458>\
**Category:** Ruby\
**Created:** [February 13, 2007, 8:12pm UTC](https://www.ruby-forum.com/t/ruby-oo-newb-subclassing-question/85458 "2007-02-13T20:12:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [February 13, 2007, 8:12pm UTC](https://www.ruby-forum.com/t/ruby-oo-newb-subclassing-question/85458/1 "2007-02-13T20:12:12Z")

</div>

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

def header(title)  
html\_head = \<\<-EOF\_HTML\_HEAD  
  
#{title}  
  
  
EOF\_HTML\_HEAD  
puts html\_head #shouldn’t this write to self(file)??  
end

def content(text)  
puts text  
end

def footer  
puts ‘’  
close  
end

end

if $0 == **FILE**  
my\_report = HtmlReport.new(‘test.html’)  
my\_report.header(‘Test report’)  
my\_report.content(‘Some text’)  
my\_report.footer  
end

But running the above returns the following error:

c:\code\ruby\sample\>ruby test2.rb  
test2.rb:11:in `write': uninitialized stream (IOError) from test2.rb:11:in`header’  
from test2.rb:28

Can somebody please tell me How it needs to be done.

Thanks  
Saleo

---

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [February 13, 2007, 8:36pm UTC](https://www.ruby-forum.com/t/ruby-oo-newb-subclassing-question/85458/2 "2007-02-13T20:36:43Z")

</div>

- Newbie (removed\_email\_address@domain.invalid) wrote:

> 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 = ‘%s’  
FOOTER = ‘’

```
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:

 Test report Some text 

Hope that helps…

---

<div class="post-metadata">

**Author:** ![newbie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/dc4da7/32.png) [@newbie](https://www.ruby-forum.com/u/newbie)\
**Post date:** [February 13, 2007, 8:40pm UTC](https://www.ruby-forum.com/t/ruby-oo-newb-subclassing-question/85458/3 "2007-02-13T20:40:45Z")

</div>

On 2/13/07, Newbie [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def initialize(filename)  
> open(filename,‘w’) #Don’t know why this does not return a file object  
> end

It does return a file, but you don’t do anything with it… initialize  
is not used to create an instance, but only to initialize de  
attributes of an instance created by Ruby when you invoke  
HtmlReport.new. So you’d have to assign the opened file to an instance  
variable of your classe to avoid loosing the reference to it.

But the problem is deeper than that. I would not want to subclass  
File. Subclassing is cool and very attractive when you first study OO,  
but it’s not the right solution in many cases.

Cheers,

Luciano
