Actions
Feature #17210
openMore readable and useful `Set#inspect`
Description
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) about 4 years 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>
,<File:README.md>
,#<CSV::Row "Name":"Bob" "Department":"Engineering" "Salary":"1000">
- of core objects, say, the
Time
is breaking all conventions being represented as2020-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
Updated by universato (Yoshimine Sato) over 3 years ago
+1
I second the proposal.
p [1, 2, 3] #=> [1, 2, 3]
p "hello" #=> "hello"
p 1.0 #=> 1.0
p({"x"=>1}) #=> {"x"=>1}
p 1..2 #=> 1..2
p nil #=> nil
p true #=> true
p :name #=> :name
p Array #=> Array
require "matrix"
p Matrix[[1, 2], [3, 4]] #=> Matrix[[1, 2], [3, 4]]
p Vector[1, 2] #=> Vector[1, 2]
The output of the major classes equal to Ruby code(literal or syntax sugar).
It is very useful and readable.
Updated by hsbt (Hiroshi SHIBATA) 8 months ago
- Status changed from Open to Assigned
Actions
Like0
Like0Like0Like0