“Accustoming Yourself to Ruby”
#1. Understand What Ruby Considers To Be True
- Every value is
trueexpectfalseandnil. - The number
0isturein Ruby. - Check if a value is nil with
value#nil?.
#2. Treat All Objects As If They Could Be nil
- Only
nil.nil?returnstrue. - You can cast a nil to a empty string with
nil.to_sor to a int withnil.to_i.
#3. Avoid Ruby’s Cryptic Perlisms
- Prefer
String#matchtostr =~. - Don’t modify global variables unless you must do.
#4. Be Aware That Constants Are Mutable
- Always
freezeconstant values to prevent them to be mutated. - To prevent assigning new values to existing constants, freeze the module they
are defined in.
#5. Pay attention to runtime warnings
- Use the
-wCLI option to enable compile and runtime warnings. Or make it
part of theRUBYOPTenv variable.
Classes, Objects and Modules
#6. Know how ruby build inheritance hierarchies
- Method lookup algo - move right, then go up, that means when you clll a method
foo.bar, you move to the right, find the class of the objectfoo, if you
findbarinfoo‘s class, then invoke it, or move tofoo‘s superclass, do
the same thing. - If no method has been found,
method_missingwill be called with the same
algo as above. - Module are classes too. Including modules silently creates singleton classes
which are inserted into the hierarchy above the including class. - Class methods are just instance methods of the class’s metaclass
#7. Be Aware of the Different Behaviors of super
- Use
superto call the overridden method. - Use
superwith no args and no parentheses is equivalent to passing it all of
the arguments which were given to the enclosing method. - Use to
super()to call overriden method withoug passing any arguments.
#8. Invoke super When Initializing Sub-classes
- Ruby doesn’t automatically call the
initializemethod in the superclass when
creating objects from a subclass - Call the overriden
initializemethod explicitly withsuper
#9. Be Alert for Ruby’s Most Vexing Parse
- Setter method must have a reciever, inside the class that defines it, the
receiver should beself - Avoid using
selfwhen call other methods inside a class.
#10. Prefer Struct to Hash for Structured Data
- When dealing with structured data which doesn’t quite justify a new class
prefer using Struct to Hash. - Assign the return value of Struct::new to a constant and treat that constant
like a class.
Demo
1 | User = Struct.new(:name, :age) # User.class = Class |