Im working with a class, containing large instance arrays.
Whenever I initialize a class like this, e.g. i = Image.new, I get a lot of junk output from my arrays (@r, @g, @b – ~300k values each).
class Image
def initialize(width=640, height=480, brightness=64)
@width, @height, @brightness = width, height, brightness
self.load('usart.dat')
end
def load(file='usart.dat')
self.reset
f = IO.read(file, @height * @width * 2, 0)
# Parsing the datafile, saving data in @r, @g, @b, @gray etc
end
return self
end
# ... More methods
end
Question is, how can I either silence the output (all results are saved to a file, never viewed in the console) or make an initializer NOT inspect self. I want to return self, since I want to stack methods, e.g. image.load('file').binary.grayscale.save(:bin).
The output you are seeing is the result of your object’s
to_s(orinspect). You can define/overwriteto_s(orinspect) for your classes to produce less output. E.g.Regarding
to_s/inspect: irb callsinspectwhich normally just callsto_s(see ruby-doc). So, definingto_sshould normally work. However, if there is aninspectnot callingto_sin the class (or its ancestors), thisinspecthas to be overwritten.