Skip to content

Instantly share code, notes, and snippets.

@crmaxx
Created July 9, 2014 12:20
Show Gist options
  • Save crmaxx/0ae3025a33c0b3fe213c to your computer and use it in GitHub Desktop.
Save crmaxx/0ae3025a33c0b3fe213c to your computer and use it in GitHub Desktop.
monkey patch for OpenStruct
class OpenStruct
# Insert/update hash data on the fly.
#
# o = OpenStruct.new
# o.merge!(:a => 2)
# o.a #=> 2
#
def merge!(other)
fails TypeError, "can't modify frozen #{self.class}", caller(1) if self.frozen?
other.each { |k, v| @table[k.to_sym] = v }
self
end
# Merge hash data creating a new OpenStruct object.
def merge(other)
o = dup
o.merge!(other)
o
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment