From: "Lipper, Matthew" Date: 2004-01-06T05:12:26+09:00 Subject: Re: Basic Syntax for Extending Instances Thank you! I'll have to do a little re-reading to understand this fully; so, your version adds an "instance instance variable" (as opposed to a "class instance" variable)? Thank again! Matt -----Original Message----- From: Austin Ziegler [mailto:austin@halostatue.ca] Sent: Monday, January 05, 2004 2:54 PM To: ruby-talk@ruby-lang.org Subject: Re: Basic Syntax for Extending Instances On Tue, 6 Jan 2004 04:42:26 +0900, Lipper, Matthew wrote: > Hey everybody, > Given the following extension to the Date class: > class Date > attr_accessor :date_precision > class << self > alias_method :old_civil, :civil > def civil(*args) > # How to make this visible? > @date_precision = args.shift > puts "#{@date_precision}" if($VERBOSE) # >> DAY_OF_MONTH > return old_civil(*args) > end > end > end > > date = Date.civil("DAY_OF_MONTH",2004,2,5) > #Doh! > puts date.date_precision # >> nil Try: class Date attr_accessor :date_precision class << self alias_method :old_civil, :civil def civil(*args) precision = args.shift _civil = old_civil(*args) _civil.date_precision = precision _civil end end end date = Date.civil("DAY_OF_MONTH", 2004, 2, 5) puts date.date_precision In your original code, you were adding @date_precision to the singleton class Date (a "class instance" variable). -austin -- austin ziegler * austin@halostatue.ca * Toronto, ON, Canada software designer * pragmatic programmer * 2004.01.05 * 14.48.27