Created
September 6, 2012 06:22
-
-
Save Heimdell/3652065 to your computer and use it in GitHub Desktop.
Methaprogramming is fun
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Structurable | |
def self.included base | |
base.class_eval do | |
def initialize hash | |
hash.each do |k, v| | |
temp = v | |
eval "@#{k} = temp" | |
self.instance_eval do | |
eval " | |
def self.#{k} | |
@#{k} | |
end | |
def self.#{k}= value | |
@#{k} = value | |
end" | |
end | |
end | |
end | |
end | |
end | |
end | |
class Structure | |
include Structurable | |
def self.test | |
test = new x: 5, | |
y: 7, | |
type: "polar", | |
axis: :vertical | |
test.x = 4 | |
print "struct, x = #{test.x}, y = #{test.y}, type = #{test.type}, axis = #{test.axis}\n" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment