https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112022-02-07T02:49:05ZRuby Issue Tracking SystemRuby master - Feature #18573: Object#pack1https://redmine.ruby-lang.org/issues/18573?journal_id=963982022-02-07T02:49:05Zknu (Akinori MUSHA)knu@ruby-lang.org
<ul></ul><p>packしたいもの(のclass)はすごく限定的なので、Objectに生やすのはどうかなあと思いました。</p> Ruby master - Feature #18573: Object#pack1https://redmine.ruby-lang.org/issues/18573?journal_id=964002022-02-07T03:02:19Zknu (Akinori MUSHA)knu@ruby-lang.org
<ul></ul><p>String#format / String#% のような感じで書式の方をレシーバにする方がよさそうですが、unpackとの対称性を考えると名前が難しいかも。</p> Ruby master - Feature #18573: Object#pack1https://redmine.ruby-lang.org/issues/18573?journal_id=964012022-02-07T03:19:04Zknu (Akinori MUSHA)knu@ruby-lang.org
<ul></ul><p>今日のOffice Hourで話したんですが、レシーバごとにそれにふさわしい書式って限定されるはずなので、何でも屋のpackではなく、たとえばIntegerならビット幅とエンディアンを指定してバイナリ表現を得るメソッド、StringならNUL-terminatedなバイナリを得るメソッド、などをそれぞれ用意すべきかも、と思いました。</p> Ruby master - Feature #18573: Object#pack1https://redmine.ruby-lang.org/issues/18573?journal_id=964192022-02-08T08:51:36Zbyroot (Jean Boussier)byroot@ruby-lang.org
<ul></ul><p>(using Google Translate to understand the discussion, sorry if I misunderstood or missed things).</p>
<p>Since <code>Object#pack1</code> might be deemed a bit too invasive, an alternative could be <code>String.pack1(format, arg)</code>, or even <code>String.pack(format, *args)</code>.</p> Ruby master - Feature #18573: Object#pack1https://redmine.ruby-lang.org/issues/18573?journal_id=1048242023-10-05T21:21:40Zjeremyevans0 (Jeremy Evans)merch-redmine@jeremyevans.net
<ul></ul><p>I developed a patch for this that implemented the feature using <code>Array.pack1</code> 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: <a href="https://github.com/ruby/ruby/pull/8598" class="external">https://github.com/ruby/ruby/pull/8598</a></p> Ruby master - Feature #18573: Object#pack1https://redmine.ruby-lang.org/issues/18573?journal_id=1048302023-10-06T14:12:50ZEregon (Benoit Daloze)
<ul></ul><p><code>Array.pack1(obj, format) -> String</code> sounds weird since there is nothing about Array in there.<br>
I think <code>String.pack1(format, obj)</code> is the best option.<br>
(<code>String#pack1(obj)</code> is confusing because of existing <code>Array#pack(format)</code> which has the reverse order).</p>
<p>(IMO this is something a JIT should optimize but I understand that's hard on CRuby).</p> Ruby master - Feature #18573: Object#pack1https://redmine.ruby-lang.org/issues/18573?journal_id=1048762023-10-12T04:21:45Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>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.<br>
The most natural candidate is Object#pack1, but I question the need to pollute the namespace for this trivial method.</p>
<p>Matz.</p> Ruby master - Feature #18573: Object#pack1https://redmine.ruby-lang.org/issues/18573?journal_id=1048942023-10-12T19:11:48Ztenderlovemaking (Aaron Patterson)tenderlove@ruby-lang.org
<ul></ul><p>matz (Yukihiro Matsumoto) wrote in <a href="#note-7">#note-7</a>:</p>
<blockquote>
<p>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.<br>
The most natural candidate is Object#pack1, but I question the need to pollute the namespace for this trivial method.</p>
<p>Matz.</p>
</blockquote>
<p>Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Add a new instruction for sending messages to ephemeral stack arrays (Closed)" href="https://redmine.ruby-lang.org/issues/18897">#18897</a> introduced a specialized instruction for Array#hash, but it created a new instruction called <code>opt_newarray_send</code>. 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.</p> Ruby master - Feature #18573: Object#pack1https://redmine.ruby-lang.org/issues/18573?journal_id=1049132023-10-13T13:00:46Zmame (Yusuke Endoh)mame@ruby-lang.org
<ul></ul><p>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 <code>0x1234.to_binary_string(4, endian: :big) #=> "\x00\x00\x12\x34"</code>.</p>