R
rbysamppi
As a beginner, I'm having trouble converting the metaprogramming in
Dwemthy's Array (http://poignantguide.net/dwemthy/) into a convenient
mixin that I can use for many classes. The trouble seems to be the
"def self." declarations, but I'm not too sure; in any
case, .metaclass and .traits don't seem to be mixing in. What should I
change?
Thanks in advance!
module Dwemthy
def self.metaclass
class << self; self; end
end
def self.traits(*given_traits)
return @traits if given_traits.empty?
attr_accessor(*given_traits)
given_traits.each do |trait|
metaclass.instance_eval do
define_method(trait) do |trait_value|
@traits ||= {}
@traits[trait] = trait_value
end
end
end
class_eval do
define_method
initialize) do
self.class.traits.each do |k, v|
instance_variable_set("@#{k}", v)
end
end
end
end
end
Dwemthy's Array (http://poignantguide.net/dwemthy/) into a convenient
mixin that I can use for many classes. The trouble seems to be the
"def self." declarations, but I'm not too sure; in any
case, .metaclass and .traits don't seem to be mixing in. What should I
change?
Thanks in advance!
module Dwemthy
def self.metaclass
class << self; self; end
end
def self.traits(*given_traits)
return @traits if given_traits.empty?
attr_accessor(*given_traits)
given_traits.each do |trait|
metaclass.instance_eval do
define_method(trait) do |trait_value|
@traits ||= {}
@traits[trait] = trait_value
end
end
end
class_eval do
define_method
self.class.traits.each do |k, v|
instance_variable_set("@#{k}", v)
end
end
end
end
end