Skip to content

Commit e4eca05

Browse files
dcubanker
authored andcommitted
optimize ObjectId#to_s
1 parent 5930c50 commit e4eca05

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

lib/bson/types/object_id.rb

+1-5
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,7 @@ def self.from_string(str)
131131
#
132132
# @return [String]
133133
def to_s
134-
str = ' ' * 24
135-
12.times do |i|
136-
str[i * 2, 2] = '%02x' % @data[i]
137-
end
138-
str
134+
@data.map {|e| v=e.to_s(16); v.size == 1 ? "0#{v}" : v }.join
139135
end
140136

141137
def inspect

test/bson/object_id_test.rb

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ def test_to_s
5151
assert_equal 24, $1.length
5252
end
5353

54+
def test_to_s2
55+
@o = ObjectId.new([76, 244, 52, 174, 44, 84, 121, 76, 88, 0, 0, 3])
56+
s = '4cf434ae2c54794c58000003'
57+
assert_equal @o.to_s, s
58+
end
59+
5460
def test_method
5561
assert_equal ObjectId.from_string(@o.to_s), BSON::ObjectId(@o.to_s)
5662
end

0 commit comments

Comments
 (0)