Last active
November 25, 2016 04:45
-
-
Save kylev/60258ef80ef8aa7ecc5252918cbdf218 to your computer and use it in GitHub Desktop.
Using an eigenclass to make some instances of BSON::ObjectId to serialize to JSON as strings.
This file contains hidden or 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
require 'json' | |
require 'bson' | |
a = BSON::ObjectId.new | |
b = BSON::ObjectId.new | |
puts a.to_json | |
# {"$oid":"5837b91f775ab9717f8d8bc9"} | |
puts b.to_json | |
# {"$oid":"5837b92c775ab9717f8d8bca"} | |
# Modify the "b" instance's eigenclass, eek?! | |
class << b | |
def as_json | |
to_s | |
end | |
end | |
puts a.to_json | |
# {"$oid":"5837b91f775ab9717f8d8bc9"} | |
puts b.to_json | |
# "5837b92c775ab9717f8d8bca" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment