Five Ruby Addictions

While it may not have the highest performance, Ruby has some features that I wish I had at my disposal in any programming language.

Method Missing
If an undefined method is called on a object, the method name and parameters get passed to method_missing (unless method_missing is missing, then an exception is raised). Method missing enables ActiveRecord (the ORM used by Rails) to do things like: User.find_by_name("daniel") without actually defining a method for each attribute on an object.

Mixins
Understanding how powerful mixins are until using them can be difficult, but they allow adding functionality to classes without extending them. In a way, this solves a need for multiple inheritance. Sure, if I want some extra functionality on ArrayList in .NET I can extend it and make my own ArrayList. But what if a client wants my added functionality along with another person's added functionality? If using Ruby, just "mixin" both our modules! In my post on how Rails processes a request I referred to how code was extremely simple and easy to read because mixins were used. I much prefer this to digging through levels of abstraction and inheritance.

Looking at Objects:
Ruby enables developers to seemingly know _everything_ about objects. Look at the documentation for Module. Some methods of interest: constants, class_variables, const_defined?, included_modules, instance_methods, method_defined?, private_instance_methods, and protected_instance_methods.

Modifying Classes at Runtime:
Rather than overriding a method, Ruby developers can alias it, redefine it, and then call the aliased method from their method. This is used extensively in Rails to modify behavior. Here are some more methods available in Module: alias_method, extend_object, class_eval, module_eval, append_features, and method_added. The method names alone are a good indication of how Ruby allows developers to have carte blanche on classes. Everything is Ruby is an object; a developer can add or modify behavior on "nil" if he or she wants to. Recently I became tired of sorting arrays like this: array.sort {|x,y| x.name <=> y.name}, so I used method missing to modify the Array class. Now I can write: array.sort_by_name.

True/False Evaluation
In Ruby, "nil" and "false" evaluate to false, everything else evaluates to true. This may not be ideal in all situations, but it is great for setting values. What if you want to add an item to an array, but you're not sure the array has been initialized? In .NET you could use: if (array == null) { array = new ArrayList(); } That's not too bad, but array ||= [] seems a lot nicer.

I know Ruby is influenced by Smalltalk and some other languages, so maybe some of these features are present elsewhere. However, often when I am working with other languages, I wish I could use method_missing, mix in behavior, modify classes at runtime (even ones like Array or Int32), and have any object but nil or false evaluate to true.