Feature #17210

More readable and useful `Set#inspect`

Added by marcandre (Marc-Andre Lafortune) 5 months ago. Updated 5 months ago.

Target version:


I would like to change Set#inspect/to_s:

# before
puts Set[1,2,3] # => "#<Set: {1, 2, 3}>"

# after
puts Set[1,2,3] # => "Set[1, 2, 3]"

This output is shorter, readable, and has the property that it corresponds to Ruby code

Updated by zverok (Victor Shepelev) 5 months ago

BTW, as a side note, it would be good to have some "style guide" for inspect behavior in Ruby. Ad-hoc rule seem to be #<ClassName some representation of content> most of the time, except for objects that have corresponding literals -- they are just represented by literals. But:

  • how the "content" should be represented, is not always consistent from class to class: #<struct A a=1, b=2>, #<Date: 2020-10-02 ((2459125j,0s,0n),+0s,2299161j)>, #<Set: {1, 2, 3}>, #<OpenStruct a=1, b=2>, #<Enumerator: [1, 2, 3]:map>, <>, #<CSV::Row "Name":"Bob" "Department":"Engineering" "Salary":"1000">
  • of core objects, say, the Time is breaking all conventions being represented as 2020-10-02 12:10:51 +0300 -- which even doesn't look as one atomic entity
  • of all stdlib I've checked, only matrix does similar to proposed: Matrix[[25, 93], [-1, 66]], Vector[1, 2], so it is a bit unusual for #inspect, though it is not necessary a bad thng

Also available in: Atom PDF