diff --git a/ext/cbson/cbson.c b/ext/cbson/cbson.c index cd54e79..42ab425 100644 --- a/ext/cbson/cbson.c +++ b/ext/cbson/cbson.c @@ -949,6 +949,7 @@ void Init_cbson() { DigestMD5 = rb_const_get(Digest, rb_intern("MD5")); rb_define_method(ObjectID, "generate", objectid_generate, 0); + rb_define_method(ObjectId, "generate", objectid_generate, 0); rb_define_method(rb_cArray, "fast_pack", fast_pack, 0); } diff --git a/lib/bson/types/object_id.rb b/lib/bson/types/object_id.rb index abae3c5..7f43c87 100644 --- a/lib/bson/types/object_id.rb +++ b/lib/bson/types/object_id.rb @@ -155,26 +155,24 @@ module BSON private - # We need to define this method only if CBson isn't loaded. - unless defined? CBson - def generate - oid = '' + # This gets overwritten by the C extension if it loads. + def generate + oid = '' - # 4 bytes current time - time = Time.new.to_i - oid += [time].pack("N") + # 4 bytes current time + time = Time.new.to_i + oid += [time].pack("N") - # 3 bytes machine - oid += Digest::MD5.digest(Socket.gethostname)[0, 3] + # 3 bytes machine + oid += Digest::MD5.digest(Socket.gethostname)[0, 3] - # 2 bytes pid - oid += [Process.pid % 0xFFFF].pack("n") + # 2 bytes pid + oid += [Process.pid % 0xFFFF].pack("n") - # 3 bytes inc - oid += [get_inc].pack("N")[1, 3] + # 3 bytes inc + oid += [get_inc].pack("N")[1, 3] - oid.unpack("C12") - end + oid.unpack("C12") end def get_inc diff --git a/lib/bson/types/objectid.rb b/lib/bson/types/objectid.rb index 8ea3863..e7eecfd 100644 --- a/lib/bson/types/objectid.rb +++ b/lib/bson/types/objectid.rb @@ -158,26 +158,24 @@ module BSON private - # We need to define this method only if CBson isn't loaded. - unless defined? CBson - def generate - oid = '' + # This gets overwritten by the C extension if it loads. + def generate + oid = '' - # 4 bytes current time - time = Time.new.to_i - oid += [time].pack("N") + # 4 bytes current time + time = Time.new.to_i + oid += [time].pack("N") - # 3 bytes machine - oid += Digest::MD5.digest(Socket.gethostname)[0, 3] + # 3 bytes machine + oid += Digest::MD5.digest(Socket.gethostname)[0, 3] - # 2 bytes pid - oid += [Process.pid % 0xFFFF].pack("n") + # 2 bytes pid + oid += [Process.pid % 0xFFFF].pack("n") - # 3 bytes inc - oid += [get_inc].pack("N")[1, 3] + # 3 bytes inc + oid += [get_inc].pack("N")[1, 3] - oid.unpack("C12") - end + oid.unpack("C12") end def get_inc