Created
July 9, 2014 12:20
-
-
Save crmaxx/0ae3025a33c0b3fe213c to your computer and use it in GitHub Desktop.
monkey patch for OpenStruct
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 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