Feature #18573
openObject#pack1
Description
ๆฆ่ฆยถ
String#unpack1 ใฎ้ใฎ Object#pack1 ใๆฌฒใใใ
่ๆฏยถ
Array#pack ใจใใใกใฝใใใใใใพใใใใฌใทใผใใผใฎ Array ใฎ่ฆ็ด ๆฐใ 1 ใคใใใชใใใจใ่ฏใใใใพใใ
[codepoint].pack('U')
[digest].pack('m0')
[mail_body].pack('M')
[ip_address].pack('N')
ๆจๆบๆทปไปใฉใคใใฉใชใผใชใฉใ็บใใฆใฟใฆใใใฉใใฉใใใใใงใใ
ใงใใใใใฎใใใชใฑใผในใงๅคๆๅฏพ่ฑกใฎใชใใธใงใฏใใใใใใ Array ใงใใใพใชใใฆใฏใใใชใใจใใใฎใฏ้ขๅใชๆฐใใใพใใ
ๆๆกยถ
String#unpack ใซๅฏพใใฆ String#unpack1 ใจใใใกใฝใใใใใใพใใใ
Array#pack ใซๅฏพใใ Object#pack1 ใจใใใกใฝใใใๆๆกใใพใใ
ใคใกใผใธใจใใฆใฏไปฅไธใฎใณใผใใฎใใใชๆใใงใใ
class Object
def pack1(template, option = {})
[self].pack(template, **option)
end
end
่ญฐ่ซใป่ชฒ้กยถ
- Object ใง่ฏใใใฉใใใฏ่ญฐ่ซใฎไฝๅฐใใใใใใจๆใใพใ
- ใกใฝใใๅใ pack1 ใง่ฏใใใฏใใใใพใใใใไปใจใใถใๅฏ่ฝๆงใฏไฝใใใจๆใใพใ
Updated by knu (Akinori MUSHA) over 3 years ago
packใใใใใฎ๏ผใฎclass๏ผใฏใใใ้ๅฎ็ใชใฎใงใObjectใซ็ใใใฎใฏใฉใใใชใใจๆใใพใใใ
Updated by knu (Akinori MUSHA) over 3 years ago
String#format / String#% ใฎใใใชๆใใงๆธๅผใฎๆนใใฌใทใผใใซใใๆนใใใใใใงใใใunpackใจใฎๅฏพ็งฐๆงใ่ใใใจๅๅใ้ฃใใใใใ
Updated by knu (Akinori MUSHA) over 3 years ago
ไปๆฅใฎOffice Hourใง่ฉฑใใใใงใใใใฌใทใผใใใจใซใใใซใตใใใใๆธๅผใฃใฆ้ๅฎใใใใฏใใชใฎใงใไฝใงใๅฑใฎpackใงใฏใชใใใใจใใฐIntegerใชใใใใๅน ใจใจใณใใฃใขใณใๆๅฎใใฆใใคใใช่กจ็พใๅพใใกใฝใใใStringใชใNUL-terminatedใชใใคใใชใๅพใใกใฝใใใใชใฉใใใใใ็จๆใในใใใใใจๆใใพใใใ
Updated by byroot (Jean Boussier) over 3 years ago
(using Google Translate to understand the discussion, sorry if I misunderstood or missed things).
Since Object#pack1
might be deemed a bit too invasive, an alternative could be String.pack1(format, arg)
, or even String.pack(format, *args)
.
Updated by jeremyevans0 (Jeremy Evans) almost 2 years ago
I developed a patch for this that implemented the feature using Array.pack1
and was going to create a new feature request for it, but I'm glad to see there already is an existing feature request for it. Here's my pull request for it: https://github.com/ruby/ruby/pull/8598
Updated by Eregon (Benoit Daloze) almost 2 years ago
Array.pack1(obj, format) -> String
sounds weird since there is nothing about Array in there.
I think String.pack1(format, obj)
is the best option.
(String#pack1(obj)
is confusing because of existing Array#pack(format)
which has the reverse order).
(IMO this is something a JIT should optimize but I understand that's hard on CRuby).
Updated by matz (Yukihiro Matsumoto) almost 2 years ago
Array.pack1 is unlikely because there is no connection between the responsibilities of the method and the Array class. I also disagree with String.pack1 for the same reason.
The most natural candidate is Object#pack1, but I question the need to pollute the namespace for this trivial method.
Matz.
Updated by tenderlovemaking (Aaron Patterson) almost 2 years ago
matz (Yukihiro Matsumoto) wrote in #note-7:
Array.pack1 is unlikely because there is no connection between the responsibilities of the method and the Array class. I also disagree with String.pack1 for the same reason.
The most natural candidate is Object#pack1, but I question the need to pollute the namespace for this trivial method.Matz.
Feature #18897 introduced a specialized instruction for Array#hash, but it created a new instruction called opt_newarray_send
. I think we could leverage that instruction for the case of an array literal + pack. That would avoid the array creation. I will try to make a patch.
Updated by mame (Yusuke Endoh) almost 2 years ago
If we introduce a new method for this, I think it would be better to design a more descriptive API instead of reusing a hacky pack format directive, such as 0x1234.to_binary_string(4, endian: :big) #=> "\x00\x00\x12\x34"
.