Recently I got my hands on Ruby Best Practices book. I found it to be a great read, especially chapters 2 and 3, are worth the price of the book.
Chapter 1 is a great introduction to test driven development, i already knew almost if not all what this chapter exposes but i found it the style to be very accesible.
Some of the good habit the author mentions
Chapter 2 is called Designing Beautiful APIs, it starts with the basics and continues to go on to some advanced tips.
It starts with
And then the chapter continues with an indepth look at blocks, showing uses of them to design an api .
Chapter 3 is the best in the book in my opinion.
It starts with an explanation of a module called BlankState , which is built-in in Ruby 1.9 as BasicObject. Basically their purporses is to avoid namespace clashes.
Then it continues with a section called building fexible instances which has a good starting tip called Making instance_eval optional . Basically what this tip mean is that when instance_evaling a block you cannot access variables outside the scope of the block, so what we do is check if the block arity is 0 or biggger. If it is zero then we can do instance_eval else we do block.call .
The following section is a must in any advanced ruby book: Method Missing and Send.
After that there is a good small tip on dual purpose accessors, which is a doubt a fellow member of ruby argentina had some days ago.
Implementing per object behaviour is a nice introudction on how to create as stubber framework.
There is next a good practice advice on how to dynamically add methods to existing classes without clashing with other libraries. Let’s say that your method names are called ln and ft:
[:in, :ft].each do |e| if instance_methods.include?(e)
raise "Method '#{e}' exists!" end
end
Modification via aliasing is the next topic, it shows that when aliasing you still conserve the old method and it is useful and logic to call it from the new method.
Finally the icing on the cake is “Building Classes and Modules programatically”. Here the author introduces the Camping framework as it shows a good example of how we can accept block parameters from the user of our code/api, in a way that supports a DSL style, using for this abstraction an anonymous class.
The author uses this example :
format :txt do def render
"Hello #{full_name} from plain text" end
end
where format is implemented like this :
def format(name, options={}, &block)
formats[name] = Class.new(options[:base] || Fatty::Format, &block)
end
Finally the author speaks about hooks like (method_added, inherited, included)
This is a great book for beginner or intermediate programmers of Ruby alike and for expert progammers it can be a good remainder of lots of already known feautures of ruby but you had forgotten to use in a while.
Comments