From: Chris Czub Date: 2007-10-26T07:19:36+09:00 Subject: Re: Need help understanding metaclassing Robert Dober wrote: > On 10/25/07, Chris Czub wrote: >> Why can't I use the attribute accessor? Rather - I understand why I >> can't, because it was created in the User class rather than the >> DefaultUser class, but how can I get access to it? >> -- >> Posted via http://www.ruby-forum.com/. >> >> > > class User > @users = {} > class << self > attr_reader :users > end > def initialize name > email = nil > loop do > email = "#{name}@#{rand 1000}" > break unless self.class.users[ email ] > end > self.class.users[ email ] = self > .... > > is this what you want? > R. I don't really want to define it in the User class, because it might not belong to a particular user... maybe I am not making sense. I am writing a testing application for many web sites that require user registrations. Each website requires similar users, but different ones might have additional fields required of a user, so my idea is to create a metaclass of user for each user configuration a website might require. So, for a web site based in the UK, the metaclass looks like this: class UKUser < User attribs :address3, :i_own_or_work_for_a_small, :my_small_business_has_its, :yes_please_send_me_the_mi, :postal first_name( "John" ) last_name( "Doe" ) email( "john.doe#{rand(100)}@abc.com" ) address1( "1 Sunshine Parkway" ) address2( "" ) address3( "" ) city( "London" ) postal( "EH15 2JJ" ) gender( "male" ) phone( "(555)123-4567" ) age( "18-25" ) my_small_business_has_its( "no" ) i_own_or_work_for_a_small( "no" ) yes_please_send_me_the_mi( "no" ) end So I am able to say user = UKUser.new user.address3("Apartment 12") user.my_small_business_has_its("yes") However, I cannot do this: user.email("bob.smith@abc.com") Any ideas? -- Posted via http://www.ruby-forum.com/.