A useful link about ruby metaclasses http://viewsourcecode.org/why/hacking/seeingMetaclassesClearly.html
Default Encoding
To change ruby encoding, run ruby with -E [coding].
Example:
1 2 |
|
Use Zip to Do Pairing of Enumerators
pending use to_a.zip or zip
Java Object Creation
Work in progress
Notes from reading “Effective Java”
Static factory method
- advantages
- with names
- not required to create new object when invoked
- can return object of any subtype of its return type
- disadvantages
- classes without public or protected constructors cannot be subclassed
1 2 3 4 5 6 7 8 9 |
|
Builder
Used when there are many constructor parameters
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
advantages:
- no long, unnamed list of parameters
- can fill up optional values
disadvantages:
speed
Suppress Stdout and Stderr When Running RSpec
Sometimes code prints to stdout, stderr. Corresponding RSpec examples print the same output, which could be messy. To suppress the output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
|
Ruby Output With Color
Colorize - prints output in console with color
sudo gem install colorize
1 2 |
|
Mocking Backtick in RSpec
`cmd` is a method in Kernel.
However, simply mocking the Kernel
object does not work.
This is because Kernel
is a module included by class Object
, its methods are available in every Ruby object.
Instead of doing this:
1 2 3 |
|
We should be doing this:
1 2 3 |
|
User-defined Metadata in Rspec
Rspec describe
, context
and it
supports metadata in the form of a hash. Syntax as follow:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
There are 2 ways that I use metadata in RSpec.
To include shared_examples / shared_context
RSpec includes shared_context and shared_examples with same metadata
1 2 3 4 5 |
|
1 2 |
|
is equivalent to
1 2 3 |
|
Use metadata as variables
1 2 3 4 5 6 7 8 9 10 11 |
|
Rspec Guide
A good guide for writing rspec
Avoid MacOS From Sleeping
Command to avoid MacOS to sleep
1
|
|