Skip to content

Instantly share code, notes, and snippets.

@kmdsbng
Created March 12, 2010 15:23
Show Gist options
  • Save kmdsbng/330414 to your computer and use it in GitHub Desktop.
Save kmdsbng/330414 to your computer and use it in GitHub Desktop.
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