Enumerations are an elegant way to make your code more readable. Unfortunately, Ruby doesn’t have a built-in enum type. (Don’t stop reading now! This story ends with hapiness and joy)
To access a class method from an instance, you have to use the reference to that class (because that’s where the method resides). The attribute reader that returns that reference is named class
I like to remind myself that a method never floats in the air and that it is always contained into an object. That’s why I prefer to specify a receiver before the name of a method… no wonder if I’m outside the object or inside the object.
First of all, let’s go back to the basics. What we generally call a class method is a method that resides at the class level. On the opposite, an instance method is a method that resides at the object level. The thing is, a class is also an object.
Have you ever come across an object that was implementing both to_i and to_int? Did you find that it was a little bit redundant (say yes please)? What is the difference between the 2?
If you come from another language, you might be tempted to use the += operator when appending to a string.
Sometimes, you want to initialize a variable to some value only if that variable is equal to nil. If you like to write your methods as short, clean and readable as possible, you can do that task in a single line instead of taking the traditionnal 3 lines approach.
Ruby truly is full of surprises. Until recently I was under the impression that Fixnum objects were like every other objects. A discussion emerging from one of my article made me realize that it was not the case. I then decided to understand once and for all what Fixnums were really made of.
Unlike many other languages, 0 isn’t qualified as false in an expression.
I made a simple list describing the 3 main things to consider when trying to understand how objects work in ruby. Feel free to add to this list as much as you want. Your comments are welcome and very appreciated.