feat: add #inspect(io : IO)

dev
LeMarsu 2019-09-30 01:50:20 +02:00
parent 7db26b8c44
commit 779f9d4616
2 changed files with 17 additions and 2 deletions

View File

@ -62,6 +62,15 @@ describe Ulid::ULID do
ulid.to_s.should eq "CNK6ET39D9NPRVBEDXR72WKKEG"
end
it "should return a custom inspect value" do
bytes = array_slice [
101, 102, 103, 104, 105, 106,
107, 108, 109, 110, 111, 112, 113, 114, 115, 116,
] of UInt8
ulid = Ulid::ULID.new(bytes)
ulid.inspect.should eq "#<Ulid::ULID CNK6ET39D9NPRVBEDXR72WKKEG>"
end
it "should instantiate from a string" do
ulid = Ulid::ULID.new "CNK6ET39D9NPRVBEDXR72WKKEG"
ulid.to_s.should eq "CNK6ET39D9NPRVBEDXR72WKKEG"

View File

@ -44,8 +44,14 @@ module Ulid
initialize(Base32.decode(str, Base32::Crockford))
end
def to_s : String
Base32.encode(@bytes, Base32::Crockford)
def to_s(io : IO) : Void
io << Base32.encode(@bytes, Base32::Crockford)
end
def inspect(io : IO) : Void
io << "#<" << self.class.name << " "
to_s(io)
io << ">"
end
def time : Time