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
Need - macros feature
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="Erik Veenstra, post: 4522468"] There is no preprocessor for Ruby. Yep, you're right. But he wants to know how to do it himself. He wants to understand Why [1]... ;] gegroet, Erik V. - [URL]http://www.erikveen.dds.nl/[/URL] [1] [URL]http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html[/URL] PS: Beer, downtown Amsterdam, tonight. No pancakes. ---------------------------------------------------------------- ## Abstraction through meta-programming ## Getters and Setters - A Demo # Consider this code: class Foo def bar [USER=75400]@bar[/USER] end def bar=(new_bar) [USER=75400]@bar[/USER] = new_bar end end # This can be rewritten to this: class Foo def self.getter(iv) define_method("#{iv}") do instance_variable_get("@#{iv}") end end def self.setter(iv) define_method("#{iv}=") do |new_value| instance_variable_set("@#{iv}", new_value) end end getter :bar setter :bar end # Which can be rewritten to this: class Module def getter(iv) define_method("#{iv}") do instance_variable_get("@#{iv}") end end def setter(iv) define_method("#{iv}=") do |new_value| instance_variable_set("@#{iv}", new_value) end end end class Foo getter :bar setter :bar end # Which can be rewritten to this: require "getter_and_setter.rb" # Fill it your self... ;] class Foo getter :bar setter :bar end # Use it: foo = Foo.new foo.bar = "Hello World!" puts foo.bar ---------------------------------------------------------------- [/QUOTE]
Verification
Post reply
Forums
Archive
Archive
Ruby
Need - macros feature
Top