ハッシュコレクションをクラスのメソッドに変換するために使用している次のコードがあります(アクティブレコードのようなものです)。私が抱えている問題は、セッターが機能していないことです。私はまだRubyに慣れていないので、少し振り返ったと思います。
class TheClass
def initialize
@properties = {"my hash"}
self.extend @properties.to_methods
end
end
class Hash
def to_methods
hash = self
Module.new do
hash.each_pair do |key, value|
define_method key do
value
end
define_method("#{key}=") do |val|
instance_variable_set("@#{key}", val)
end
end
end
end
end
メソッドが作成され、クラスで読み取ることができますが、設定しても機能しません。
myClass = TheClass.new
item = myClass.property # will work.
myClass.property = item # this is what is currently not working.