From: Austin Ziegler Date: 2007-03-04T08:25:52+09:00 Subject: Re: attr_reader explained On 3/3/07, Tim Becker wrote: > On 3/3/07, libsfan01 wrote: > > can someone explain how attr_reader works? > > i can't find a good explanation anywhere. > It's built in to the language, so it 'works' by magic for all > practical purposes, it's implemented in `object.c` if you want to take > a look at the implementation. If you wanted to implement it in Ruby, > you do something like this: Actually, that's not true. It's written in C, but that's not the same as being built into the language. It's a method on Module, so it *can* be overridden. >> class Module >> alias old_attr_reader attr_reader >> def attr_reader(*names) >> puts "Making attribute readers for #{names.join(", ")}" >> old_attr_reader *names >> end >> end => nil >> class Foo >> attr_reader :bar >> attr_reader :baz, :quux >> end Making attribute readers for bar Making attribute readers for baz, quux -austin -- Austin Ziegler * halostatue@gmail.com * http://www.halostatue.ca/ * austin@halostatue.ca * http://www.halostatue.ca/feed/ * austin@zieglers.ca