Bug #6159
closedEnumerable::Lazy#inspect
Description
Hello,
Enumerable::Lazy#inspect is undefined right now and relies on Enumerator#inspect:
(1..5).lazy # => #<Enumerator::Lazy: #<Enumerator::Generator:0x00000101a2f9e8>:each>
I think it would be nice to be similar to (direct) Enumerator#inspect:
(1..5).each # => #<Enumerator: 1..5:each>
So something like: #<Enumerator::Lazy: 1..5:each> or #<Enumerator::Lazy: #<Enumerator: 1..5:each>>
It would also be nice to show the chaining, like Enumerator does:
(1..5).select.map.flat_map # => #<Enumerator: #<Enumerator: #<Enumerator: 1..5:select>:map>:flat_map>
What do you think?
Updated by shugo (Shugo Maeda) over 12 years ago
Benoit Daloze wrote:
So something like: #<Enumerator::Lazy: 1..5:each> or #<Enumerator::Lazy: #<Enumerator: 1..5:each>>
It would also be nice to show the chaining, like Enumerator does:
(1..5).select.map.flat_map # => #<Enumerator: #<Enumerator: #<Enumerator: 1..5:select>:map>:flat_map>
What do you think?
I agree that It would be nice. However, unlike Enumerator, Enumerator::Lazy doesn't have enough information, so it needs to have some information only for inspect. Is it so important to make inspect rich?
Updated by shugo (Shugo Maeda) over 12 years ago
- Status changed from Open to Feedback
- Assignee set to shugo (Shugo Maeda)
Updated by Eregon (Benoit Daloze) over 12 years ago
I agree that It would be nice. However, unlike Enumerator, Enumerator::Lazy doesn't have enough information, so it needs to have some information only for inspect.
Is it so important to make inspect rich?
I'm not sure, but it would certainly be helpful when debugging.
On the other hand, if that would impact significantly performance or code (maintainability), it's probably not worth it.
I guess I'm not used to a core Ruby class without a nice #inspect.
Updated by shugo (Shugo Maeda) over 12 years ago
- Status changed from Feedback to Closed
- % Done changed from 0 to 100
This issue was solved with changeset r35124.
Benoit, thank you for reporting this issue.
Your contribution to Ruby is greatly appreciated.
May Ruby be with you.
-
enumerator (enumerator_inspect): include the original receiver and
method name of Enumerator::Lazy in the result of inspect.
[ruby-core:43345] [Bug #6159] -
enumerator (InitVM_Enumerator): don't use rb_define_alias for
some methods such as collect in order to make rb_frame_this_func()
return the correct method names.
Updated by shugo (Shugo Maeda) over 12 years ago
Eregon (Benoit Daloze) wrote:
I agree that It would be nice. However, unlike Enumerator, Enumerator::Lazy doesn't have enough information, so it needs to have some information only for inspect.
Is it so important to make inspect rich?I'm not sure, but it would certainly be helpful when debugging.
On the other hand, if that would impact significantly performance or code (maintainability), it's probably not worth it.I guess I'm not used to a core Ruby class without a nice #inspect.
I guess it wouldn't impact performance or maintainability so much, and fixed Enumerable::Lazy#inspect so that the following code:
(1..10).lazy.select(&:odd?).map(&:to_s).inspect
to return the following string:
"#<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: 1..10>:select>:map>"
Updated by Eregon (Benoit Daloze) over 12 years ago
Awesome, thanks!
Your answer raises another question: Would it not be more readable if #inspect was closer to the code?
(1..10).lazy.select(&:odd?).map(&:to_s).cycle(2).inspect
Instead of
"#<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: 1..10>:select>:map>:cycle(2)>"
Something like
"#<Enumerator::Lazy: 1..10.select.map.cycle(2)>"
?
Updated by shugo (Shugo Maeda) over 12 years ago
Eregon (Benoit Daloze) wrote:
Your answer raises another question: Would it not be more readable if #inspect was closer to the code?
(1..10).lazy.select(&:odd?).map(&:to_s).cycle(2).inspect
Instead of
"#<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: 1..10>:select>:map>:cycle(2)>"
Something like
"#<Enumerator::Lazy: 1..10.select.map.cycle(2)>"
?
The former can distinguish the following lazy enumerators, but the latter can't:
p (1..10).lazy.select(&:odd).map(&:to_s)
#=> #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator::Lazy: 1..10>:select>:map>
p (1..10).select.lazy.map(&:to_s)
#=> #<Enumerator::Lazy: #<Enumerator::Lazy: #<Enumerator: 1..10:select>>:map>
p (1..10).select.map.lazy
#=> #<Enumerator::Lazy: #<Enumerator: #<Enumerator: 1..10:select>:map>>
So I prefer the current behavior.
Updated by Eregon (Benoit Daloze) over 12 years ago
Right, thank you for the explanation.
Updated by mame (Yusuke Endoh) over 12 years ago
Hello,
2012/3/25, shugo (Shugo Maeda) redmine@ruby-lang.org:
So I prefer the current behavior.
I agree with you, as a core team member.
It is a simple and straightforward implementation.
But I also agree with Benoit, as a user.
The output is actually a bit verbose.
I guess the fault is the prefix "#<Enumerator::Lazy: ".
It is too long and unfriendly for human eyes.
--
Yusuke Endoh mame@tsg.ne.jp
Updated by shugo (Shugo Maeda) over 12 years ago
Hello,
2012/3/27 Yusuke Endoh mame@tsg.ne.jp:
So I prefer the current behavior.
I agree with you, as a core team member.
It is a simple and straightforward implementation.
But I also agree with Benoit, as a user.
The output is actually a bit verbose.I guess the fault is the prefix "#<Enumerator::Lazy: ".
It is too long and unfriendly for human eyes.
Hmm...could you make a counter proposal? We may be able to choose a
shorter prefix such as "#<lazy", in which case, however, the class
name information would be lost.
--
Shugo Maeda