Ruby/Class/inspect
Материал из Wiki.crossplatform.ru
(Различия между версиями)
Версия 17:10, 26 мая 2010
Override the inspect the method to provide a better print out
class Dog def inspect "<A Dog named #{@name} who"s #{@age} in dog years.>" end def to_s inspect end end spot = Dog.new() puts spot.inspect
Print out a class
class Dog def initialize(name, age) @name = name @age = age * 7 #Compensate for dog years end end spot = Dog.new("Spot", 2.1) spot.inspect # => "#<Dog:0xb7c16bec @name=\"Spot\", @age=14.7>"