<
character
and the name of the superclass to your class statement.
We then create a subclass using class Child < Parent
.
The <
notation
means we’re creating a subclass of the thing on the right; the fact
that we use less-than presumably signals that the child class is
supposed to be a specialization of the parent.
class Point3D < Point # Define class Point3D as a subclass of Point end
[~/rubytesting/TheRubyProgrammingLanguage/Chapter7ClassesAndModules]$ cat subclassing_a_struct.rb class Punto < Struct.new("Point3D", :x, :y, :z) # Superclass struct give us accessor methods, ==, to_s, etc. def modulo Math.sqrt(x*x+y*y+z*z) end end if $0 == __FILE__ p = Punto.new(1,2,3) puts p.x puts p.modulo endEjecución:
[~/rubytesting/TheRubyProgrammingLanguage/Chapter7ClassesAndModules]$ ruby subclassing_a_struct.rb 1 3.7416573867739413