From: Rob Biedenharn Date: 2009-01-24T07:38:06+09:00 Subject: Re: class 'initialize' method not working ??? Tom, Since it looks like your Ruby code is written in a distinctly non-Ruby- esque style, I thought I'd show you how I'd expect most Rubyists would write your Dump_db On Jan 23, 2009, at 4:57 PM, Tom Cloyd wrote: > Florian, thanks for your response. It's a bit of a mind blower, > although I think I understand it. I continue to discover that > classes have a number of subtleties which my reliable old workhorse > procedural Ruby (all I've done until this week) simply doesn't have. > It's fun learning about them, though...mostly! > > Here's a small class from the same project I'm working on - and it > works fine. The only structural difference I can see is that ALL > code in this second class is wrapped in method definitions. So, if I > understand you correctly, the code in the second method below is NOT > part of the class definition evaluation, but is subsumed under the > definition of a method, thus preventing the problem I was having > with the other code. My question: Do I have this right? > > # dump setnet data to files > class Dump_db > def initialize( logging_now, log, db_array ) > @logging_now = logging_now > @log = log > @db_array = db_array > end def dump > @db_array.each do |cnt| > begin #start error trapping > dbnm = cnt[ 0 ] > dbobj = cnt[ 1 ] > if @logging_now: @log.info( dbnm + ' output to file started' ) > end > fn = dbnm + '.yml' > open( fn, 'w' ) {|i| YAML.dump( dbobj, i )} > status = ( ( dbobj.length - 1 ).to_s + dbnm +' written to > file' ) > puts "> "+status > if @logging_now: @log.info status end > rescue Exception => e > if @logging_now: @log.error( "#{e}" ) end > if @logging_now: @log.error e.backtrace end #pinpoints error > location end > puts "> ERROR logged to log file..." > result = nil > end > end > end > end > > This is all very interesting, and if I understand it right, it's > something that I have not seen pointed out in any of the references > I've been studying. Kind of a major point. > > Thanks again for you help (and you also, Lucas). > > Tom # dump setnet data to files class Dump_db def initialize(db_array, log=nil) @db_array = db_array @log = log end def dump @db_array.each do |dbnm,dbobj| begin #start error trapping @log.info( dbnm + ' output to file started' ) if @log open(dbnm + '.yml', 'w' ) {|i| YAML.dump( dbobj, i )} status = ( ( dbobj.length - 1 ).to_s + dbnm +' written to file' ) puts "> "+status @log.info status if @log rescue Exception => e if @log @log.error( "#{e}\n " ) @log.error e.backtrace.join("\n ") puts "> ERROR logged to log file..." end #pinpoints error location end end end end end Note in particular: The order of arguments to Dump_db.new (and thus, initialize) have changed. If you don't intend to log, you can just say: Dump_db.new(your_db_array) There was no separation between @log and @logging_now, so just rely on whether a log was given. The use of a : to mean "then" has been deprecated. However, you can put a conditional modifier at the end of a statement: if @log @log.info( "stuff" ) end is exactly: @log.info( "stuff" ) if @log You probably want to separate the lines in the backtrace, so look at what I've done to the rescue block. Typically, you don't rescue Exception, but rather StandardError. That's the default, too, so you can just: rescue => e to mean rescue StandardError => e since most of the exceptions that are not inherited from StandardError you won't want to catch in most cases. -Rob Rob Biedenharn http://agileconsultingllc.com Rob@AgileConsultingLLC.com