Forums
New posts
Search forums
Members
Current visitors
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
Forums
Archive
Archive
Ruby
attr_reader, default attribute value, and rdoc of attribute
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
[QUOTE="cibercitizen1, post: 4624796"] 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 [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
attr_reader, default attribute value, and rdoc of attribute
Top