Skip to content

Instantly share code, notes, and snippets.

@kylev
Last active November 25, 2016 04:45
Show Gist options
  • Save kylev/60258ef80ef8aa7ecc5252918cbdf218 to your computer and use it in GitHub Desktop.
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.
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