From: "David A. Black" Date: 2009-01-20T02:10:29+09:00 Subject: Re: In Depth: attr_reader/writer/accessor Hi -- On Mon, 24 Nov 2008, Matthew Madson wrote: > David A. Black wrote: >> Hi -- >> >> On Mon, 24 Nov 2008, Matthew Madson wrote: >> >>> Can anyone explain exactly what happens when the interpreter interprets >>> attr_reader and its kin? >>> >>> E.G. where is the method located (in which module, class) and what does >>> it do with the symbols in order to expand the method call into an >>> instance method of the calling class object? >> >> attr_reader and friends are private instance methods of the class >> Module: >> >>>> Module.private_instance_methods(false).grep(/attr/) >> => ["attr_accessor", "attr_writer", "attr_reader", "attr"] >> >> which means that as long as self is an instance of Module or one of >> its subclasses, those methods can be called. The Class class is a >> subclass of Module, so instances of Class can execute those methods >> too. That's what's happening when you do: >> >> class C >> attr_accessor :x >> end >> >> As for what it does: see Greg's Ruby implementation, and also the >> original C code which is in eval.c (look for the definition of >> rb_attr). It basically calls rb_define_method, which is a general >> method for adding methods to classes. >> >> I used implementing attr_* as a metaprogramming exercise in a Ruby >> course I taught last week, and it was very instructive. Amazing what >> you can do in Ruby, without even getting terribly convoluted or >> opaque. >> >> >> David > > Not that I don't appreciate the other responses but this is exactly what > I was looking for. Thank you for your help everyone. Also, David, can > you recommend any reference texts for Ruby that delve into this much > detail for other similar concepts. I'm nearly finished with Programming > Ruby: Pragmatic Programmers and I feel like I want a bit more. Sorry -- this got lost in the inbox pipeline. I have an answer for you, though: The Well-Grounded Rubyist (by me), due out in March. See http://www.manning.com/black2. There's no one-stop-shopping source for all such stuff but I think you'll find a good bit 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!