From: swille Date: 2005-10-28T11:15:54+09:00 Subject: Re: "Reflecting" on my self.n00b On 10/27/05, Gregory Brown wrote: > For a little more clarification on usage: > > irb(main):027:0> class Foo > irb(main):028:1> def initialize() > irb(main):029:2> @url, @username, @password, @certificate, @owner = nil > irb(main):030:2> end > irb(main):031:1> attr_accessor :url, :username, :password, > :certificate, :owner > irb(main):032:1> end > => nil > irb(main):033:0> a = Foo.new > => # @owner=nil, @url=nil> > irb(main):034:0> a.url = "http://newhavenrubyists.org" > => "http://newhavenrubyists.org" > > and if you need to extend something > > irb(main):036:0> class Bar < Foo > irb(main):037:1> end > => nil > irb(main):038:0> b = Bar.new > => # @owner=nil, @url=nil> > irb(main):039:0> b.url = "http://stonecode.org/blog" > => "http://stonecode.org/blog" > > > This seems to work great, thanks! I swear, this is the first way that I thought of, but somehow I missed all of the instance_variable calls. Mental note, look harder. :O) Still though, I'm unsure of whether this is a smart thing to do. Any thoughts on that? I'd definitely like to learn the right way to do stuff. class ServiceProfile def initialize @sp_url = nil @sp_username = nil @sp_password = nil @sp_certificate = nil @sp_owner = nil end def load_certificate @sp_certificate.read end def call_service(&block) end end class MyService < ServiceProfile def initialize @sp_extra = nil super end end svc_profile = MyService.new #ServiceProfile.new sp_re = Regexp.new('^@sp_(\w+)') svc_profile.instance_variables.sort.reverse.each { |var| if var.match(sp_re) print "Enter #{Regexp.last_match(1).capitalize}: " value = $stdin.gets.chomp svc_profile.instance_variable_set(var, value) end } svc_profile.instance_variables.sort.reverse.each { |var| if var.match(sp_re) puts svc_profile.instance_variable_get(var) end }