From: cibercitizen1 Date: 2008-11-26T21:00:23+09:00 Subject: Re: attr_reader, default attribute value, and rdoc of attribute I don't know about rdoc, but perhaps the following might help. See first class Test and usage, to find out if this is what you need #!/usr/bin/env ruby #----------------------------------------------- #----------------------------------------------- module MyAccessor def my_attribute (nou, defval=nil) if defval == nil code ="def #{nou} (s=nil) @#{nou} = s || @#{nou} end" elsif defval.class==String code ="def #{nou} (s=nil) @#{nou} = s || @#{nou} || '#{defval}' end" else code ="def #{nou} (s=nil) @#{nou} = s || @#{nou} || #{defval} end" end puts "defining ", code class_eval code end # def my_attr end # module #---------------------------------------- class Test extend MyAccessor my_attribute(:color, "red") my_attribute(:width, 20) my_attribute(:text, "no name") def initialize (&block) instance_eval(&block) end end #---------------------------------- # usage puts " -------------- " t = Test.new { color "blue" } puts " -------------- " puts t.color # => blue puts t.width # => 20 puts t.text # => no name t.width 50 puts t.width # => 50