Skip to content

Commit

Permalink
Make ULID#encode and ULID#to_s returning same value again
Browse files Browse the repository at this point in the history
This partially reverts #236, because the optimized benefit is higher for usecase. I think...
  • Loading branch information
kachick committed Aug 7, 2022
1 parent 7812bb4 commit 5b4a340
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/ulid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ def initialize(milliseconds:, entropy:, integer:, encoded:)

# @return [String]
def encode
@encoded.dup
@encoded
end
alias_method(:to_s, :encode)

Expand Down
2 changes: 1 addition & 1 deletion test/core/test_ulid_class.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ def test_normalize
assert(normalized.frozen?)
assert_not_same(normalized, ULID.normalize(normalized))

assert_false(ULID.normalize(normalized).frozen?)
assert_true(ULID.normalize(normalized).frozen?)

# Ensure the string is not modified in parser
assert_false(downcased.frozen?)
Expand Down
15 changes: 6 additions & 9 deletions test/core/test_ulid_instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,20 +194,17 @@ def test_lexicographically_sortable
def test_encode
ulid = ULID.parse('01ARZ3NDEKTSV4RRFFQ69G5FAV')
assert_equal('01ARZ3NDEKTSV4RRFFQ69G5FAV', ulid.encode)
assert_not_same(ulid.encode, ulid.encode)
assert_equal(ulid.encode, ulid.encode)
assert_false(ulid.encode.frozen?)
assert_not_same(ulid.to_s, ulid.encode)
assert_same(ulid.encode, ulid.encode)
assert_true(ulid.encode.frozen?)
assert_same(ulid.to_s, ulid.encode)
end

def test_to_s
ulid = ULID.parse('01ARZ3NDEKTSV4RRFFQ69G5FAV')
assert_equal('01ARZ3NDEKTSV4RRFFQ69G5FAV', ulid.to_s)
assert_not_same(ulid.to_s, ulid.to_s)
assert_equal(ulid.to_s, ulid.to_s)
assert_false(ulid.to_s.frozen?)
assert_not_same(ulid.encode, ulid.to_s)
assert_equal(ulid.encode, ulid.to_s)
assert_same(ulid.to_s, ulid.to_s)
assert_true(ulid.to_s.frozen?)
assert_same(ulid.encode, ulid.to_s)
end

def test_inspect
Expand Down
2 changes: 1 addition & 1 deletion test/core/test_ulid_usecase.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,6 @@ def test_marshal_and_unmarshal
dumped = Marshal.dump(frozen)
unmarshaled = Marshal.load(dumped)
assert_true(unmarshaled.frozen?)
assert_false(unmarshaled.to_s.frozen?)
assert_true(unmarshaled.to_s.frozen?)
end
end

0 comments on commit 5b4a340

Please sign in to comment.