There is Ruby code no mortal being is meant to see, let alone use, for it is pure evil. If you wish to save your immortal soul, then read no further!
*SIGH* - Don’t say I didn’t warn you.
Since your soul is doomed anyway, I’ll let you in on some of the evil things that are possible thanks to Evil Ruby.
When Apple released OS X on x86 hardware, thus causing Hell to freeze over quicker than you can say “Power PC”, a solution was needed immediately. It fell upon Belial, acting through his agents Florian Gross and Mauricio Fernandez 1, to solve the problem. Using his fiendish powers, he concocted the Object#unfreeze method:
hell = "Hell".freeze hell.frozen? # true hell.unfreeze hell.frozen? # false hell.sub!("Hell", "Heaven") hell # => "Heaven"
This is evil, but not half as evil as the machinations of Vassago, who was determined to convert objects of one type into objects of a completely different type! His diabolical plan? To convert dogs into cats! 2
class Cat def mew puts "meow" end end class Dog def bark puts "woof" end end dog = Dog.new p dog.class # => Dog p dog.class.instance_methods(false) # ['bark'] dog.class = Cat # Zing! p dog.class # => Cat p dog.class.instance_methods(false) # ['mew']
Gaap, on the other hand, preferred a much more subtle approach. Instead of altering an object’s class, he decided that it would be much more insidious to allow methods to bind themselves to objects that normally don’t allow it. Using the Object#force_bind method, he could then force dogs to meow! Here’s the code, using our Cat class from above:
dog = Dog.new mew = Cat.instance_method(:mew) p mew # #<UnboundMethod: Cat#mew> mew.bind(dog).call # TypeError mew.force_bind(dog).call # => 'meow'
In a move that stunned Lucifer herself, Asmodius developed a technique for converting classes to modules on the fly!3 The Object.as_module method allows you to mixin classes without subclassing or resorting to delegation.4 For example:
class Bar include Array.as_module end b = Bar.new b.push(1,2,3) p b # => [1,2,3]
Lastly, Beelzebub wanted me to mention some of the less evil methods that were added for your convenience. For example you can get the singleton of a class by using the Object#singleton_class method:
class Foo end f = Foo.new f.singleton_class # => #<<Class:#<Foo:0x568890>>
You can also check to see if you're dealing with a direct value (Fixnum, Symbol, true, false, nil) using the Object#direct_value? method:
x = 666 y = 'hell' z = nil x.direct_value? # true y.direct_value? # false z.direct_value? # true
This is but a sampling of the evil things possible with evil.rb. There are also methods for implementing a form of multiple inheritance, changing a class’ superclass, and sharing instance variables between two objects.
Real world uses
All kidding aside, there are some potential uses for the features in evil.rb. For example, you can use it for deferred instantiation and proxy objects 5. It could also be used for implementing certain features of Aspect Oriented Programming, such as cuts.6 There are undoubtedly many other evil uses that this author has not even begun to fathom, but which you may wish to investigate.
May the Lord have mercy on your pathetic soul.7
End of Line.
1 Both sold their souls many years ago. Long story.
2 Cats, as we all know, are natural agents of Satan.
3 Not one of Beelzebub’s flies obviously.
4 Scholars have often noted the similarity between “Asmodius” and “as_module”.
7 This article is satire. The author does not actually believe that Florian Gross or Mauricio Fernandez sold their souls (to his knowledge, anyway). Nor does he believe in the notion that cats are natural agents of Satan, the four princes of Hell, or that OS X actually runs on x86 hardware.