#11. Create namespace by nesting code in Module
- Use modules to create namespece by nesting definiton inside them.
- Mirror you namesapece structure to your dir structure.
- Use
::
to fully qualify a top-level constant.
#12. Understand the Different Flavors of Equality
- Use
==
to test if two obejcts represent the same value.
1 | 1 == `1.0` # true |
- Use
eql?
to test if two objects represent the same value and also the same
type.
1 | 1 == `1.0` # false |
Use
equal?
to test if two objects are the same object (with same object_id),
so never override this method.===
-== || =~ || is_a?
1 | 'a'==='a' #true - 'a'=='a' |
#13. Implement Comparison via <=> and the Comparable Module
- Implement object ordering by defining a “<=>” operator and including the Comparable module.
- The “<=>” operator should return nil if the left operand can’t be compared with the right.
- If you implement “<=>” for a class you should consider aliasing eql? to “==”, especially if you want instances to be usable as hash keys. In which case you should also override the hash method. NOTE.
==
does NOT test type equity.
#14. Share Private State Through Protected Methods
- Protected methods can be called with an explicit receiver from objects of the same class
#15. Prefer Class Instance Variables to Class Variables
#16. Duplicate Collections Passed as Arguments Before Mutating Them
- Method arguments in Ruby are passed as references, not values
- The dup and clone methods only create shallow copies.
1 | class User |
- For most objects, Marshal can be used to create deep copies when needed.
1 | arr3 = Marshal.load Marshal.dump arr |
#17. Use the Array Method to Convert nil and Scalar Objects into Arrays
1 | Array(nil) # [] |
#18. Consider Set for Efficient Element Inclusion Checking
require 'set'
before you use it.
#19. Know How to Fold Collections with reduce
- Always use a starting value for the accumulator.
1 | [1,2,3].reduce(0,:+) #6 |
- The block given to reduce should always return an accumulator. It’s fine to mutate the current accumulator, just remember to return it from the block”.
1 | [1,2,3].reduce({}) do |map, e| |