From: Florian Gilcher Date: 2009-01-24T05:58:23+09:00 Subject: Re: class 'initialize' method not working ??? -----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-----