[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

[Fwd: Re: Preferred monkeypatching technique]

John Johnson

7/17/2006 7:07:00 PM


> Daniel Berger wrote:

> Well, the idea is that the 3rd party software is going to be using
> whatever classes it has always used, so just creating a subclass doesn't
> get me anywhere because the 3rd party app (i.e. Rails) doesn't give a
> hoot about my BigNozzleFiretruck.

You could use a factory for that, and allow the plugin to override the
factory's creater method.

#!/usr/bin/env ruby

class FireTruck
def say
puts "truck"
end
end

class FireTruckFactory
def FireTruckFactory.create
return FireTruck.new
end
end

# First plugin
class Pumper < FireTruck
def say
puts "Pumper here."
end
end

class FireTruckFactory
def FireTruckFactory.create
return Pumper.new
end
end

# Second plugin
class HookAndLadder < FireTruck
def say
puts "Hook and ladder here."
end
end

class FireTruckFactory
def FireTruckFactory.create
return HookAndLadder.new
end
end

#try it
ft = FireTruckFactory.create
ft.say # => Hook and ladder here.

Regards,
JJ