From: "David A. Black" Date: 2009-01-24T07:18:06+09:00 Subject: Re: class 'initialize' method not working ??? Hi Tom -- On Sat, 24 Jan 2009, Tom Cloyd wrote: > 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? [snip] There's another way to look at this, which might be helpful. The way you tell which object an instance variable belongs to is simple: it belongs to self. That's always true. Whenever you see: @x if you know what self is at that moment, you know whose instance variable @x is. So, for example: class MyClass puts "self is: #{self}" @x = 1 end Output: self is MyClass self, in this context, is the class object MyClass itself. That means that @x belongs to MyClass; it is an instance variable of MyClass, in MyClass's capacity as an instance of Class (which all classes are). Here, self is different: class MyClass def a_method puts "self is: #{self}" @x = 1 end end obj = MyClass.new obj.a_method # self is: # In one context, self is a class object called MyClass. In the other context (inside a method definition), self is an instance of MyClass. (It's sort of "an instance to be named later"; the method only gets executed once an instance of MyClass exists and calls it.) Classes and their instances do not share instance variables, any more than any other objects do (which is to say, not at all). I've always said that the answer to 75% of all questions about Ruby is: "Because classes are objects." :-) That's the central point here. A class *itself* can have instance variables because, in addition to be an "object factory", a class is also, in its own right, an object. > 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. Check out "The Well-Grounded Rubyist" -- it's definitely in there :-) David -- David A. Black / Ruby Power and Light, LLC Ruby/Rails consulting & training: http://www.rubypal.com Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2) http://www.wishsight.com => Independent, social wishlist management!