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”.
5 http://weblog.jamisbuck.org/2004/4/23/side-effects-of-deferred-instantiation
6 http://wiki.rubygarden.org/Ruby/page/show/AspectOrientedRuby,
http://wiki.rubygarden.org/Ruby/page/show/AspectOrientedRuby/NotesRoughDrafts
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.

Heh, it's funny to see this article posted on the very date of my birthday. Thank you! :)
By the way, evil.rb is looking for a new maintainer. Mauricio and me really haven't invested much time into this in the last years. If you feel like contributing to the project by adding a new feature or improving existing ones, please contact us. I'll willingly add you to the project even if you just want to do a small contribution.
Oh, and evil.rb might seem useless at first, but it really is quite convenient for learning about how Ruby works internally. I've tried shifting focus into that direction a bit. If you'd like to make it more useful for that kind of work please contribute!
Florian,
I had no idea it was your birthday! Funny coincidence.
You're right about evil.rb being a good way to learn about Ruby internals (not to mention the 'dl' package). Maybe a separate post on that is in order, though I may use ruby-inline instead if I go that route.
As for feature requests, I'm wondering what happened Object#become. Too many issues?
I don't think I would be much use as a maintainer, btw, but thanks for the offer. :)
Yup, when cleaning things up for 1.9 compatibility I noticed that the unit tests for Object#become randomly segfaulted. It would probably have taken days of debugging to make it work correctly.
And heh, to be honest the 'you' was refering to the reader. I think you would a be a *lot* of use as the maintainer, but I understand you are probably even busier. :)
Thanks again for the cool article!
The article might be more accurate than you believe }:-)
In line with the new emphasis on evil.rb as an educational tool, I'm assembling a list of pointers to articles and ML threads featuring evil.rb:
A taste of evil.rb: using DL to unfreeze objects
Low-level introspection to save brain bits: Ruby object model, class hierarchy and method dispatching
Easy AOP right now using evil.rb
Prototype-based OOP
Prototype-based Ruby