I am reading Sandi Metz's POODR and have come across a coding principle that I don't quite understand. Here is the code:
class Bicycle
attr_reader :size, :chain, :tire_size
def initialize(args = {})
@size = args[:size] || 1
@chain = args[:chain] || 2
@tire_size = args[:tire_size] || 3
post_initialize(args)
end
end
class MountainBike < Bicycle
attr_reader :front_shock, :rear_shock
def post_initialize(args)
@front_shock = args[:front_shock]
@rear_shock = args[:rear_shock]
end
end
mb = MountainBike.new(front_shock: 4, rear_shock: 5)
puts mb.size
puts mb.chain
puts mb.tire_size
puts mb.front_shock
puts mb.rear_shock
This code will output 1,2,3,4,5
for its respective attributes. What I don't understand is the method look up.
When a mountain bike is instantiated, because it does not have its own initialize
method, it will travel up the method lookup chain onto its super class (Bicycle
). But now from there, it seems that Bicycle then moves back down into MountainBike's post_initialize method. Instead of continuing up the method chain, how can it go back down? Is post_initialize
a ruby keyword like initialize
in that it serves some sort of special function? Is there some other ruby introspection methods I can use to see what is going on?