Tom Cloyd
1/23/2009 9:57:00 PM
Florian Gilcher wrote:
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
>
> On Jan 23, 2009, at 9:09 PM, Tom Cloyd wrote:
>
>> I'm baffled by this error, after a hour+ of experimentation with
>> solutions. I assume it's a compiler error (right term in Ruby?), as
>> I'm not even getting access to ruby-debug.
>
>
> Its not a ruby error.
>
>> Code snippet:
>> =======
>> def main
>>
>> opdb = Open_db.new( [1,2,3] )
>>
>> end
>>
>> class Open_db
>> def initialize( dblist )
>> @dbs = dblist
>> end
>>
>> @dbs.each do |cnt| # <= line producing the error
>> db_lbl = cnt[0]
>> db_nm = cnt[1]
>> end
>> end
>>
>
> That might be irritating at first, but you actually refer to two
> different variables hier.
>
> First, in #initialize, you refer to an instance variable called @dbs
> in the context of an object instance (the instance of Open_db).
>
> The second part with @dbs, which is evaluated when loading the class
> definition is refering to an instance variable in the context of a
> class definition (as classes in ruby are objects by themselves, this
> is perfectly valid). The problem is, that in class context, @dbs has
> not been initialized. Unitialized instance variables are nil by
> definition => calling each on it fails.
>
> Feel free to ask further questions.
>
> Regards,
> Florian
>
> - --
> Florian Gilcher
>
> smtp: flo@andersground.net
> jabber: Skade@jabber.ccc.de
> gpg: 533148E2
>
> -----BEGIN PGP SIGNATURE-----
> Version: GnuPG v1.4.8 (Darwin)
>
> iEYEARECAAYFAkl6L9sACgkQyLKU2FMxSOJU6ACfZQlLGOXbY8Rq3ChHRfpOWWBy
> c5EAoJ9dVgjh4EagntgbEyr8Z1DGn8kq
> =46uW
> -----END PGP SIGNATURE-----
>
>
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
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~