From e4eca054472c9ee9fa69e988075078a29dd68d18 Mon Sep 17 00:00:00 2001 From: "David A. Cuadrado" Date: Mon, 29 Nov 2010 18:21:58 -0500 Subject: [PATCH] optimize ObjectId#to_s --- lib/bson/types/object_id.rb | 6 +----- test/bson/object_id_test.rb | 6 ++++++ 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/bson/types/object_id.rb b/lib/bson/types/object_id.rb index 7b97699..3567118 100644 --- a/lib/bson/types/object_id.rb +++ b/lib/bson/types/object_id.rb @@ -131,11 +131,7 @@ module BSON # # @return [String] def to_s - str = ' ' * 24 - 12.times do |i| - str[i * 2, 2] = '%02x' % @data[i] - end - str + @data.map {|e| v=e.to_s(16); v.size == 1 ? "0#{v}" : v }.join end def inspect diff --git a/test/bson/object_id_test.rb b/test/bson/object_id_test.rb index 01e076a..9f533d4 100644 --- a/test/bson/object_id_test.rb +++ b/test/bson/object_id_test.rb @@ -51,6 +51,12 @@ class ObjectIdTest < Test::Unit::TestCase assert_equal 24, $1.length end + def test_to_s2 + @o = ObjectId.new([76, 244, 52, 174, 44, 84, 121, 76, 88, 0, 0, 3]) + s = '4cf434ae2c54794c58000003' + assert_equal @o.to_s, s + end + def test_method assert_equal ObjectId.from_string(@o.to_s), BSON::ObjectId(@o.to_s) end