You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Allow Int#chr to accept an optional encoding for converting a byte value to the encodings defined character for said byte value. String.new(bytes : Bytes, encoding : String) already allows specifying a custom encoding, so I feel like Int#chr should as well.
The text was updated successfully, but these errors were encountered:
The idea does not sound very convincing. It seems like a very rare use case. String.new should be reasonably easy to use. Or you can implement a custom single character encoding method going through an IO if you need to. But I don't see this as a common problem.
Can you explain why you think this would be generally useful as a standard lib feature?
Ths two methods are not related; String.new relates to UTF-8 (or arbitrary) byte sequences, whereas Int#chr deals with UTF-8 codepoints. Not all encodings define their characters in terms of codepoints, so such a generalization isn't possible here. For example, "あ" in Shift JIS is the byte sequence 0x82 0xA0; it is not the result of mapping 0xA082 or 0x82A0 to a code point, and it most certainly does not imply that 0x82.chr(encoding: "Shift-JIS") + 0xA0.chr(encoding: "Shift-JIS") is a well-formed way to produce a Shift JIS-encoded character.
Right. It seems you can pass an encoding to Ruby's chr, but in my mind that doesn't make much sense. Maybe it only works for one byte, but more bytes aren't supported? But I don't see that as very useful.
Maybe a method Char.new(byte, encoding) and Char.new(bytes, encoding) would make sense, where a single char is decoded from a byte or a byte sequence?
Discussion
Allow
Int#chr
to accept an optional encoding for converting a byte value to the encodings defined character for said byte value. String.new(bytes : Bytes, encoding : String) already allows specifying a custom encoding, so I feel likeInt#chr
should as well.The text was updated successfully, but these errors were encountered: