From: "austin (Austin Ziegler) via ruby-core" Date: 2023-12-18T02:03:37+00:00 Subject: [ruby-core:115770] [Ruby master Feature#20069] Buffer class in stdlib Issue #20069 has been updated by austin (Austin Ziegler). pynix (Pynix wang) wrote: > ruby use `String` to deal with bytes, this cause error on irb "invalid byte sequence in utf-8" > > can we get a builtin class like Buffer or Bytes that represent as hex string ```ruby class Bytes < String def inspect bytes.pack("c*").unpack1("H*") end end s = Bytes.new(SecureRandom.bytes(10)) ``` What might be more interesting than suggesting an unnecessary class, but suggesting a different `#inspect` if the encoding is `ASCII-8BIT` or `BINARY` (because `SecureRandom.bytes(10).encoding # => #`, which will eventually be called `Encoding:BINARY`).I���m not sure what such inspect should be, because the inspect that I wrote above for `Bytes` is both inefficient and incorrect (because the representation is not what is shown on `#inspect`, which differs from other strings). Maybe: ```ruby class Bytes < String def inspect "#<#{encoding}:#{bytes.pack("c*").unpack1("H*")}>" end end Bytes.new(SecureRandom.bytes(10)) => # ``` ---------------------------------------- Feature #20069: Buffer class in stdlib https://bugs.ruby-lang.org/issues/20069#change-105707 * Author: pynix (Pynix wang) * Status: Open * Priority: Normal ---------------------------------------- ruby use `String` to deal with bytes, this cause error on irb "invalid byte sequence in utf-8" can we get a builtin class like Buffer or Bytes that represent as hex string -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/