Bug #6159

Enumerable::Lazy#inspect

Added by Benoit Daloze almost 3 years ago. Updated almost 3 years ago.

[ruby-core:43345]
Status:Closed
Priority:Normal
Assignee:Shugo Maeda
ruby -v:ruby 2.0.0dev (2012-03-15 trunk 35042) [x86_64-darwin10.8.0] Backport:

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: # or #>

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?

Associated revisions

Revision 35124
Added by Shugo Maeda almost 3 years ago

  • enumerator (enumerator_inspect): include the original receiver and
    method name of Enumerator::Lazy in the result of inspect.
    [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.

Revision 35124
Added by Shugo Maeda almost 3 years ago

  • enumerator (enumerator_inspect): include the original receiver and
    method name of Enumerator::Lazy in the result of inspect.
    [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.

History

#1 Updated by Shugo Maeda almost 3 years ago

Benoit Daloze wrote:

So something like: # or #>

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?

#2 Updated by Shugo Maeda almost 3 years ago

  • Status changed from Open to Feedback
  • Assignee set to Shugo Maeda

#3 Updated by Benoit Daloze almost 3 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.

#4 Updated by Shugo Maeda almost 3 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.
    [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.

#5 Updated by Shugo Maeda almost 3 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:

"#:select>:map>"

#6 Updated by Benoit Daloze almost 3 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
"#:select>:map>:cycle(2)>"
Something like
"#"
?

#7 Updated by Shugo Maeda almost 3 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
"#:select>:map>:cycle(2)>"
Something like
"#"
?

The former can distinguish the following lazy enumerators, but the latter can't:

p (1..10).lazy.select(&:odd).map(&:to_s)
#=> #:select>:map>
p (1..10).select.lazy.map(&:to_s)
#=> #>:map>
p (1..10).select.map.lazy
#=> #:map>>

So I prefer the current behavior.

#8 Updated by Benoit Daloze almost 3 years ago

Right, thank you for the explanation.

#9 Updated by Yusuke Endoh almost 3 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

#10 Updated by Shugo Maeda almost 3 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

Also available in: Atom PDF