https://bugs.ruby-lang.org/https://bugs.ruby-lang.org/favicon.ico?17113305112017-09-15T17:33:49ZRuby Issue Tracking SystemRuby master - Feature #13904: getter for original information of Enumeratorhttps://bugs.ruby-lang.org/issues/13904?journal_id=667012017-09-15T17:33:49Zzverok (Victor Shepelev)zverok.offline@gmail.com
<ul></ul><p>Super-upvote!</p>
<p>In fact, recently I became rather concerned with a lack of "inspectability" of Ruby's own objects (like "how <code>#format</code> would parse this string and what groups it has", or internal structure of Regexp and so on).</p> Ruby master - Feature #13904: getter for original information of Enumeratorhttps://bugs.ruby-lang.org/issues/13904?journal_id=667062017-09-15T22:36:21ZEregon (Benoit Daloze)
<ul></ul><p>I agree, I think it's a good idea to expose such information when it is available in #inspect and it is user-provided (not internal).</p> Ruby master - Feature #13904: getter for original information of Enumeratorhttps://bugs.ruby-lang.org/issues/13904?journal_id=667322017-09-17T10:42:38Zknu (Akinori MUSHA)knu@ruby-lang.org
<ul><li><strong>Assignee</strong> set to <i>knu (Akinori MUSHA)</i></li></ul> Ruby master - Feature #13904: getter for original information of Enumeratorhttps://bugs.ruby-lang.org/issues/13904?journal_id=667382017-09-18T05:31:33Zknu (Akinori MUSHA)knu@ruby-lang.org
<ul></ul><p>Enumerator is about abstracting enumeration and encapsulation of the source is by design. If we exposed the guts of an Enumerator, people would start to look into the enclosed object and do (I think are) evil things for optimization and specialization, which should be in the opposite direction of abstraction. If you expect something other than a one-way, not necessarily rewindable stream, I think you should create a new model that describes the aspect by sub-classing Enumerator or making a mix-in.</p>
<p>In summary, if you want to handle a numeric enumerator specially, I'd say you should create a class for that instead of making surrounding methods guess or find out what they receive is special by invesigating its internals. It shouldn't be a good way to introduce a new idea to the language.</p> Ruby master - Feature #13904: getter for original information of Enumeratorhttps://bugs.ruby-lang.org/issues/13904?journal_id=667472017-09-18T12:35:54Zzverok (Victor Shepelev)zverok.offline@gmail.com
<ul></ul><blockquote>
<p>Enumerator is about abstracting enumeration and encapsulation of the source is by design.</p>
</blockquote>
<p>Well, to be honest, it seems like "forced encapsulation", and is against Ruby's hackable nature.<br>
I understand your concerns, but if <em>something</em> that even on <code>#inspect</code> looks like <code>#<ClassName x:y></code> provides no access to <code>x</code> and <code>y</code> (though definitely knows them) it just "doesn't feel right".</p>
<p>I believe that composite objects (and enumerator by nature is composite: source + enumeration method) should provide access to what they are composed of, for bad or for good. It is completely possible new ways of interaction and new libraries would emerge in this case.</p> Ruby master - Feature #13904: getter for original information of Enumeratorhttps://bugs.ruby-lang.org/issues/13904?journal_id=667482017-09-18T23:04:22Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><blockquote>
<p>evil things for optimization and specialization</p>
</blockquote>
<p>Reminds me of good old evil.rb - now I am suddenly all for it! Just kidding. :-)</p>
<p>I have no particular pro or con opinion here but I think that one of ruby's philosophy is to put trust into the ruby hacker to do what he/she wants to do, e. g. duck patching modifying any of ruby's core functionality (class String, Array, Hash etc...), use .send() or .instance_variable_get() at leisure (I used this today to obtain @logged_in content in Net::FTP instances ... I think there is another method to access it, such as open?, but I saw the output of @logged in via pp, and thought I wanted to get the value, so I used instance_variable_get()) etc... ruby is very, very flexible. It's like a lispy-smalltalk-something OOP language. Encapsulation is possible but it also restricts what you can do (logically), so it is not always ruby's philosophy - to let the ruby hacker do what he/she wants, if he/she really wants to. This philosophy is also, I think, why constants in ruby are not 100% constants that can never be changed - ruby allows you to change constants. While the word constant may thus be a misnomer, I think it fits very well into the ruby philosophy overall.</p>
<p>In regards to encapsulation, that is why, I think, .public_send() was added and people can use that for the public/private distinction more clearly. I myself love .send(), freedom to all objects and especially all the duckling objects. \o/</p>
<p>By the way, the idea of composite objects reminds me a bit of what matz wrote about interface objects (specification where objects could/should respond to the same method-behaviour ... all ducks should be able to quack and swim ... so if something quacks and swims, even if it is a wolf disguised as a sheep, it may be treated to be an "acceptable duck").</p>
<p>But as said, I really am neutral here either way. :-)</p>
<p>Perhaps the ruby core team can discuss this a bit (I don't know japanese so I do not know what thoughts are conveyed above).</p> Ruby master - Feature #13904: getter for original information of Enumeratorhttps://bugs.ruby-lang.org/issues/13904?journal_id=667532017-09-19T03:47:21Zknu (Akinori MUSHA)knu@ruby-lang.org
<ul></ul><p>Well, this issue states that the motivation for introducing the getter methods is for getting return values of Range#step(n) to be handled specially by some libraries or individual methods, and as I said it is to me a typical misuse of such accessors I've been thinking of and I believe there's a better way to achieve the goal, so I can't buy that argument.</p>
<p>I can hardly believe you'd be happy (at the cost of introducing a new feature) if you had to check <code>x.instance_of?(Enumerator) && x.receiver.is_a?(Numeric) && x.method_name == :step</code> every time your method was passed an object that might be a step object. It really looks like you are doing some hack as the last resort because you don't have anything better yet. If stepped range objects are useful, they deserve a new class for their own.</p> Ruby master - Feature #13904: getter for original information of Enumeratorhttps://bugs.ruby-lang.org/issues/13904?journal_id=675042017-10-22T05:30:08Zknu (Akinori MUSHA)knu@ruby-lang.org
<ul><li><strong>Is duplicate of</strong> <i><a class="issue tracker-2 status-5 priority-4 priority-default closed" href="/issues/3714">Feature #3714</a>: Add getters for Enumerator</i> added</li></ul> Ruby master - Feature #13904: getter for original information of Enumeratorhttps://bugs.ruby-lang.org/issues/13904?journal_id=675062017-10-22T05:31:40Zknu (Akinori MUSHA)knu@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Rejected</i></li></ul><p>Closing because the given use case does not seem to be a good one to support the proposal.</p> Ruby master - Feature #13904: getter for original information of Enumeratorhttps://bugs.ruby-lang.org/issues/13904?journal_id=675072017-10-22T05:32:22Zknu (Akinori MUSHA)knu@ruby-lang.org
<ul></ul><p>(And it is also a duplicate of <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Add getters for Enumerator (Closed)" href="https://bugs.ruby-lang.org/issues/3714">#3714</a>)</p> Ruby master - Feature #13904: getter for original information of Enumeratorhttps://bugs.ruby-lang.org/issues/13904?journal_id=675402017-10-23T06:18:27Zmrkn (Kenta Murata)muraken@gmail.com
<ul><li><strong>Related to</strong> <i><a class="issue tracker-2 status-6 priority-4 priority-default closed" href="/issues/14044">Feature #14044</a>: Introduce a new attribute `step` in Range</i> added</li></ul> Ruby master - Feature #13904: getter for original information of Enumeratorhttps://bugs.ruby-lang.org/issues/13904?journal_id=709982018-03-15T06:35:37Zmrkn (Kenta Murata)muraken@gmail.com
<ul><li><strong>Status</strong> changed from <i>Rejected</i> to <i>Assigned</i></li><li><strong>Assignee</strong> changed from <i>knu (Akinori MUSHA)</i> to <i>matz (Yukihiro Matsumoto)</i></li></ul><p>(Continue from <a class="issue tracker-2 status-6 priority-4 priority-default closed" title="Feature: Introduce a new attribute `step` in Range (Rejected)" href="https://bugs.ruby-lang.org/issues/14044">#14044</a>)</p>
<p>Following today's developers meeting, I propose to introduce a subclass of Enumerator.</p>
<p>This is the example implementation of the subclass:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">class</span> <span class="nc">Enumerator::ArithmeticSequence</span> <span class="o"><</span> <span class="no">Enumerator</span>
<span class="nb">attr_reader</span> <span class="ss">:first</span> <span class="c1"># already in Enumerator</span>
<span class="nb">attr_reader</span> <span class="ss">:last</span> <span class="c1"># newly introduced</span>
<span class="nb">attr_reader</span> <span class="ss">:step</span> <span class="c1"># newly introduced</span>
<span class="k">def</span> <span class="nf">inspect</span>
<span class="c1"># appropriate string representation of this class instances</span>
<span class="k">end</span>
<span class="k">end</span>
</code></pre>
<p><code>Integer#step</code> and <code>Range#step</code> of integer ranges should return the instance of <code>Enumerator::ArithmeticSequence</code> like:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="nb">p</span> <span class="mi">1</span><span class="p">.</span><span class="nf">step</span> <span class="c1">#=> (1.step)</span>
<span class="nb">p</span> <span class="mi">1</span><span class="p">.</span><span class="nf">step</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span> <span class="c1">#=> (1.step(10))</span>
<span class="nb">p</span> <span class="mi">1</span><span class="p">.</span><span class="nf">step</span><span class="p">(</span><span class="mi">10</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span> <span class="c1">#=> (1.step(10, by:2))</span>
<span class="nb">p</span> <span class="mi">1</span><span class="p">.</span><span class="nf">step</span><span class="p">(</span><span class="ss">by: </span><span class="mi">2</span><span class="p">)</span> <span class="c1">#=> (1.step(by:2))</span>
<span class="nb">p</span> <span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">10</span><span class="p">).</span><span class="nf">step</span> <span class="c1">#=> (1.step(10))</span>
<span class="nb">p</span> <span class="p">(</span><span class="mi">1</span><span class="o">..</span><span class="mi">10</span><span class="p">).</span><span class="nf">step</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="c1">#=> (1.step(10, by:2))</span>
</code></pre>
<p>I think introducing this class doesn't introduce incompatibility from the current behaviors, but increase the usefulness of the results of <code>Integer#step</code> and <code>Range#step</code>.</p> Ruby master - Feature #13904: getter for original information of Enumeratorhttps://bugs.ruby-lang.org/issues/13904?journal_id=709992018-03-15T06:38:28Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul><li><strong>Assignee</strong> changed from <i>matz (Yukihiro Matsumoto)</i> to <i>mrkn (Kenta Murata)</i></li></ul><p>Sounds reasonable. Accepted.</p>
<p>Matz.</p> Ruby master - Feature #13904: getter for original information of Enumeratorhttps://bugs.ruby-lang.org/issues/13904?journal_id=710212018-03-15T12:12:41ZEregon (Benoit Daloze)
<ul></ul><p>This seems fine but very specific.<br>
I still think exposing an Enumerator's receiver, method name and arguments is better as it is more general, intuitive and useful.</p>
<p>I don't think there is anything wrong with checking the condition mentioned above by <a class="user active user-mention" href="https://bugs.ruby-lang.org/users/8">@knu (Akinori MUSHA)</a> in e.g. pycall.rb for #[].</p> Ruby master - Feature #13904: getter for original information of Enumeratorhttps://bugs.ruby-lang.org/issues/13904?journal_id=710242018-03-15T13:19:40ZHanmac (Hans Mackowiak)hanmac@gmx.de
<ul></ul><p>what about something when you don't have method name or arguments to expose?</p>
<p>like that idea with a combined Enumerator?<br>
that does interate [1,2,3] first, then ["a", "b", "c"] and so on.</p>
<p>for such thing i don't think that there is much to expose</p> Ruby master - Feature #13904: getter for original information of Enumeratorhttps://bugs.ruby-lang.org/issues/13904?journal_id=733382018-08-06T09:08:36Zmrkn (Kenta Murata)muraken@gmail.com
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Closed</i></li></ul><p>Applied in changeset trunk|r64205.</p>
<hr>
<p>enumerator.c: Introduce Enumerator::ArithmeticSequence</p>
<p>This commit introduces new core class Enumerator::ArithmeticSequence.<br>
Enumerator::ArithmeticSequence is a subclass of Enumerator, and<br>
represents a number generator of an arithmetic sequence.</p>
<p>After this commit, Numeric#step and Range#step without blocks<br>
returned an ArithmeticSequence object instead of an Enumerator.</p>
<p>This class introduces the following incompatibilities:</p>
<ul>
<li>You can create a zero-step ArithmeticSequence,<br>
and its size is not ArgumentError, but Infinity.</li>
<li>You can create a negative-step ArithmeticSequence from a range.</li>
</ul>
<p><a href="/issues/13904">[ruby-core:82816]</a> [Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: getter for original information of Enumerator (Closed)" href="https://bugs.ruby-lang.org/issues/13904">#13904</a>]</p> Ruby master - Feature #13904: getter for original information of Enumeratorhttps://bugs.ruby-lang.org/issues/13904?journal_id=739772018-09-11T01:01:26Zmrkn (Kenta Murata)muraken@gmail.com
<ul><li><strong>Has duplicate</strong> <i><a class="issue tracker-2 status-6 priority-4 priority-default closed" href="/issues/15092">Feature #15092</a>: Provide step count in Range constructor</i> added</li></ul>