optimize ObjectId#to_s

This commit is contained in:
David A. Cuadrado 2010-11-29 18:21:58 -05:00 committed by Kyle Banker
parent 5930c500b4
commit e4eca05447
2 changed files with 7 additions and 5 deletions

View File

@ -131,11 +131,7 @@ module BSON
# #
# @return [String] # @return [String]
def to_s def to_s
str = ' ' * 24 @data.map {|e| v=e.to_s(16); v.size == 1 ? "0#{v}" : v }.join
12.times do |i|
str[i * 2, 2] = '%02x' % @data[i]
end
str
end end
def inspect def inspect

View File

@ -51,6 +51,12 @@ class ObjectIdTest < Test::Unit::TestCase
assert_equal 24, $1.length assert_equal 24, $1.length
end 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 def test_method
assert_equal ObjectId.from_string(@o.to_s), BSON::ObjectId(@o.to_s) assert_equal ObjectId.from_string(@o.to_s), BSON::ObjectId(@o.to_s)
end end