Effective Ruby: 48 Specific Ways to Write Better Ruby (Effective Software Development Series)
Peter J. Jones
If you’re an skilled Ruby programmer, Effective Ruby may help you harness Ruby’s complete energy to write down extra strong, effective, maintainable, and well-performing code. Drawing on approximately a decade of Ruby adventure, Peter J. Jones brings jointly forty eight Ruby most sensible practices, professional counsel, and shortcuts—all supported through reasonable code examples.
Jones deals useful recommendation for every significant sector of Ruby improvement, from modules to reminiscence to metaprogramming. all through, he uncovers little-known idioms, quirks, pitfalls, and intricacies that powerfully effect code habit and performance.
Each merchandise includes particular, actionable, basically equipped instructions; cautious recommendation; targeted technical arguments; and illuminating code examples. whilst a number of recommendations exist, Jones indicates you ways to settle on the person who will paintings top on your situation.
Effective Ruby might help you systematically enhance your code—not via blindly following principles, yet via completely knowing Ruby programming techniques.
Key positive aspects of this concise consultant include
How to prevent pitfalls linked to Ruby’s occasionally brilliant idiosyncrasies
What you have to learn about inheritance hierarchies to effectively use Rails (and different huge frameworks)
How to exploit misunderstood tips on how to do amazingly beneficial issues with collections
Better how you can use exceptions to enhance code reliability
Powerful metaprogramming methods (and concepts to avoid)
Practical, effective trying out strategies, together with MiniTest Unit and Spec Testing
How to reliably deal with RubyGem dependencies
How to utilize Ruby’s reminiscence administration and profiling tools
How to enhance code potency by means of figuring out the Ruby interpreter’s internals
The module, no longer the superclass. if you run into this case you’ve most likely made a significant layout mistakes although. think about using composition rather than inheritance. eventually, the very last thing i would like to alert you approximately is how tremendous interacts with method_missing. when you give it some thought, so long as you simply ever use great in a style which has an analogous identify as one other approach within the inheritance hierarchy it won’t contain method_missing. nonetheless, if you’re an extremely sleepy driving force.
Which has legitimate constructors you may anticipate that every one copies within the hierarchy are chained jointly rather than overriding each other. That’s now not the case in Ruby. contemplate this: type father or mother attr_accessor(:name) def initialize @name = "Howard" finish finish classification baby < mum or dad attr_accessor(:grade) def initialize @grade = eight finish finish taking a look at Parent#initialize, it’s transparent that in case you create a brand new mum or dad item the @name example variable can be initialized with a default price. It’s additionally transparent.
Returns precise if the precise operand can be the fake item. nonetheless, utilizing fake because the correct operand would possibly not paintings as anticipated seeing that different sessions can override the Object#== process and unfasten the comparability: irb> classification undesirable def == (other) real finish finish irb> fake == Bad.new ---> fake irb> Bad.new == fake ---> precise in fact, anything like this might be lovely foolish. yet in my adventure, that suggests it’s prone to take place. (By the way in which, we’ll get extra into the “==” operator in merchandise.
types for these dependencies. The reduce sure is straightforward, choose the earliest model of the dependency that you simply understand to paintings properly together with your package deal. the higher sure it a bit extra difficult since you have a number of assorted offerings. Going opposed to the recommendation during this merchandise, you could easily put out of your mind the higher certain. That’s now not really nice to the group notwithstanding since you haven't any method of understanding that your gem goes to proceed to paintings for all destiny models of its dependencies. the best factor to do.
The heap. in the course of a sweeping part, if all slots in a web page are published, the rubbish collector could choose to go back that web page again to the working system’s loose shop. which means, as saw from the perspective of the working process, the reminiscence footprint of a Ruby strategy will seem to develop and curb through the years. From the interior, a Ruby strategy grows and shrinks through pages and never person gadgets. The variety of preliminary slots may have an impact at the time it takes to begin a Ruby.