Skip to content

Instantly share code, notes, and snippets.

@Heimdell
Created September 6, 2012 06:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Heimdell/3652065 to your computer and use it in GitHub Desktop.
Save Heimdell/3652065 to your computer and use it in GitHub Desktop.
Methaprogramming is fun
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