N
Newbie
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
<html>
<head><title>#{title}</title>
</head>
<body>
EOF_HTML_HEAD
puts html_head #shouldn't this write to self(file)??
end
def content(text)
puts text
end
def footer
puts '</body></html>'
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
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
<html>
<head><title>#{title}</title>
</head>
<body>
EOF_HTML_HEAD
puts html_head #shouldn't this write to self(file)??
end
def content(text)
puts text
end
def footer
puts '</body></html>'
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