###by Antwan Wimberly###
class Base
def print_value(value)
puts value.to_s
end
end
class DerivedClass < Base; end
DerivedClass.new.print_value(99) # => 99
# we'll use this bag of functionality to override the parent in a consistent way
module CustomPrint
def print_value(value)
count_to_ten = proc {
1.upto(10) { puts "counting to 10." }
}
count_to_ten.call
end
end
# this guy needs to do more than just print a value...he needs to count to 10
class CountToTen < Base
include CustomPrint
end
CountToTen.new.print_value(99) # => counting to 10.
# => counting to 10.
# => counting to 10.
# => ....7 more times
# but what if for some strange reason I wanted to have the best of both worlds?
class BestOfBoth < Base
extend CustomPrint
def print_value(value)
super
# we could also call BestOfBoth.print_value(value)
self.class.print_value(value)
end
end
BestOfBoth.new.print_value(99) # => 99
# => counting to 10.
# => counting to 10.
# => counting to 10.
# => ....7 more times