From: Tom Cloyd Date: 2009-01-24T08:01:19+09:00 Subject: Re: class 'initialize' method not working ??? Rob Biedenharn wrote: > 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 > > Wow, this IS my day for Ruby.study - I'm deeply appreciative. Learning the idiom takes time and exposure, but it's usually worth it (at least in Spanish and French...and English!) I will carefully study what you've written, you may be sure. It won't be wasted - far from it. Essentially all my code is written in direct response to a pressing need, so I'm always trying simply to 'get it working and move on'. I really NEED what this current project will do for me, and have been needing it for weeks. That pressing need can interfere with pure learning, alas. My code reflects my background in procedural programming - again, always in response to some pressing need. But I'm very interested in the elegance of "The Ruby Way" - it's what brought me to Ruby in the first place. Again, thanks for your thought, time, and effort - you've given me a considerable gift. I will make every attempt to carry what I'm learning today forward into the next code I write (and also will be cleaning up some already written stuff - need to set a good example for myself!) t. -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist Bellingham, Washington, U.S.A: (360) 920-1226 << tc@tomcloyd.com >> (email) << TomCloyd.com >> (website) << sleightmind.wordpress.com >> (mental health weblog) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~