# This class represents an image. Many image operations are chainable methods
# that return the processed image.
class Image
def lighter()
copy = Image.new(self)
# make copy lighter
copy
end
def darker()
copy = Image.new(self)
# make copy darker
copy
end
def scale(s)
scaled = Image.new( @width * s,@height * s )
# you get the idea
scaled
end
def save( file )
# ...
end
end
# this class knows how to log annotated images.
class Logger
def log(line, image, fname)
@logfile.puts("<div><div>#{line}</div><img src='#{fname}'></div>")
image.save(fname)
end
end
# this class does complicated image processing and we want to log intermediate results
class ImageProcessor
attr_accessor :logger
def something_complicated(input_image)
@logger.log("input", input_image, "input.png")
tmp = input_image.lighter()
@logger.log("lighter", tmp, "lighter.png")
tmp = tmp.scale(2.0)
@logger.log("scaled x 2", tmp, "scaled.png")
# ....
tmp
end
# I could add a log method to image and chain method calls like:
def something_complicated2(input_image)
input_image.
log(@logger, "input", "input.png").
lighter().
log(@logger, "lighter", "lighter.png").
scale(2.0).
log(@logger, "scaled x 2", "scaled.png")
end
# but I really would rather want it to look something like:
def something_complicated3(input_image)
input_image.
log("input", "input.png"). # << not mentioning @logger
lighter().
log("lighter", "lighter.png").
scale(2.0).
log("scaled x 2", "scaled.png")
end
end