Project

General

Profile

Actions

Feature #7349

open

Struct#inspect needs more meaningful output

Added by postmodern (Hal Brodigan) over 11 years ago. Updated over 6 years ago.

Status:
Assigned
Target version:
-
[ruby-core:49328]

Description

When inheriting directly from Struct.new, Class#ancestors shows a meaningless anonymous Class:

class Point < Struct.new(:x, :y)

  def distance
    ((x ** 2) + (y ** 2)) ** 0.5
  end

end

Point.ancestors
# => [Point, #<Class:0x007fe204a1a228>, Struct, Enumerable, Object, Kernel, BasicObject]

Perhaps, the anonymous Class could list the Struct's fields?

#<Class:x, y>

Updated by shyouhei (Shyouhei Urabe) over 11 years ago

Sounds nice to me. +1

Updated by claytrump (Clay Trump) over 11 years ago

I like it too. Could even be:

 Point.ancestors
 # => [Point, Struct.new(:x, :y), Struct, Enumerable, Object, Kernel,

BasicObject]

--

Updated by Eregon (Benoit Daloze) over 11 years ago

It might be worth pointing out that this should not happen if the Struct generated class is assigned to a constant (and so one level of inheritance is not unused):

Point = Struct.new(:x, :y) do
def distance
Math.hypot(x,y)
end
end

Actions #4

Updated by mame (Yusuke Endoh) over 11 years ago

  • Tracker changed from Bug to Feature

Updated by mame (Yusuke Endoh) over 11 years ago

  • Status changed from Open to Assigned
  • Assignee set to matz (Yukihiro Matsumoto)

Updated by mame (Yusuke Endoh) over 11 years ago

This is not a bug. This ticket has been moved to the feature tracker.

I'm not against this proposal, but I don't think that people normally check the singleton class to know Struct fields. You may want to use Struct.#members:

p Point.members #=> [:x, :y]

It is shorter than ancestors.

--
Yusuke Endoh

Actions #7

Updated by naruse (Yui NARUSE) over 6 years ago

  • Target version deleted (2.6)
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0