Permalink
Browse files

optimize ObjectId#to_s

  • Loading branch information...
dcu authored and banker committed Nov 29, 2010
1 parent 5930c50 commit e4eca054472c9ee9fa69e988075078a29dd68d18
Showing with 7 additions and 5 deletions.
  1. +1 −5 lib/bson/types/object_id.rb
  2. +6 −0 test/bson/object_id_test.rb
@@ -131,11 +131,7 @@ def self.from_string(str)
#
# @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
@@ -51,6 +51,12 @@ def test_to_s
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

0 comments on commit e4eca05

Please sign in to comment.