Please don't do this. Ever. But let's teach people how to shoot themselves on the foot:
class BasicObject
def self.new(*args)
if instance_methods.member? :initialise
allocate.initialise(*args)
else
super
end
end
end
So what's happening here: We just overwrote the .new definition. By default it calls #allocate and then #initialize passing down the *args you sent to it.
.allocate allocates the memory for the object but does not call #initialize over it, so we check if the object has a #initialise instance method defined, if it has it gets called, otherwise default behaviour (super) is used.
initializeandinitialiseare defined in a class? Which one has "preference"? Also, you might use a library or gem where someone legally defines aself.initialise, which however is not intended to be used as constructor, and at the same time has not defined ainitializemethod, because he is happy with the default. I certainly would not want to debug errors resulting from this kind of problem....asd98fi12jk2naoshujibdh1sdasd2asdfa2rfxs? There's a reason why people don't use lolcode language for serious projects... It's annoying, as isinitialize