Created
March 12, 2010 15:23
-
-
Save kmdsbng/330414 to your computer and use it in GitHub Desktop.
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
class Struct | |
unless self.respond_to? :org_new_for_access_instance_variable | |
class << self | |
alias_method :org_new_for_access_instance_variable, :new | |
def new(*ary, &block) | |
st = org_new_for_access_instance_variable(*ary, &block) | |
st.module_eval { | |
alias_method :org_init, :initialize | |
def initialize(*ary) | |
org_init(*ary) | |
obj_singleton = class << self; self end | |
self.class.members.each {|m| | |
self.instance_variable_set '@' + m, self.__send__(m) | |
obj_singleton.__send__(:attr_accessor, m) | |
} | |
end | |
} | |
st | |
end | |
end | |
end | |
end | |
A = Struct.new(:a) | |
class A | |
def b | |
@a | |
end | |
end | |
A.new('hoge').b #=> "hoge" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment