• Complain

Peter J. Jones - Effective Ruby : 48 Specific Ways to Write Better Ruby

Here you can read online Peter J. Jones - Effective Ruby : 48 Specific Ways to Write Better Ruby full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2015, publisher: Addison-Wesley, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

No cover
  • Book:
    Effective Ruby : 48 Specific Ways to Write Better Ruby
  • Author:
  • Publisher:
    Addison-Wesley
  • Genre:
  • Year:
    2015
  • Rating:
    4 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 80
    • 1
    • 2
    • 3
    • 4
    • 5

Effective Ruby : 48 Specific Ways to Write Better Ruby: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "Effective Ruby : 48 Specific Ways to Write Better Ruby" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

If youre an experienced Ruby programmer, Effective Ruby will help you harness Rubys full power to write more robust, efficient, maintainable, and well-performing code. Drawing on nearly a decade of Ruby experience, Peter J. Jones brings together 48 Ruby best practices, expert tips, and shortcuts -- all supported by realistic code examples. Jones offers practical advice for each major area of Ruby development, from modules to memory to metaprogramming. Throughout, he uncovers little-known idioms, quirks, pitfalls, and intricacies that powerfully impact code behavior and performance. Each item contains specific, actionable, clearly organized guidelines; careful advice; detailed technical arguments; and illuminating code examples. When multiple options exist, Jones shows you how to choose the one that will work best in your situation. Effective Ruby will help you systematically improve your code -- not by blindly following rules, but by thoroughly understanding Ruby programming techniques. Key features of this concise guide include: How to avoid pitfalls associated with Rubys sometimes surprising idiosyncrasies; What you should know about inheritance hierarchies to successfully use Rails (and other large frameworks); How to use misunderstood methods to do amazingly useful things with collections; Better ways to use exceptions to improve code reliability; Powerful metaprogramming approaches (and techniques to avoid); Practical, efficient testing solutions, including MiniTest Unit and Spec Testing; How to reliably manage RubyGem dependencies; How to make the most of Rubys memory management and profiling tools; How to improve code efficiency by understanding the Ruby interpreters internals. - Publisher. Read more...

Peter J. Jones: author's other books


Who wrote Effective Ruby : 48 Specific Ways to Write Better Ruby? Find out the surname, the name of the author of the book and a list of all author's works by series.

Effective Ruby : 48 Specific Ways to Write Better Ruby — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "Effective Ruby : 48 Specific Ways to Write Better Ruby" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make
1. Accustoming Yourself to Ruby

With each programming language you learn, its important to dig in and discover its idiosyncrasies. Ruby is no different. While it borrows heavily from the languages that proceeded it, Ruby certainly has its own way of doing things. And sometimes those ways will surprise you.

We begin our journey through Rubys many features by examining its unique take on common programming ideas. That is, those that impact every part of your program. With these items mastered, youll be prepared to tackle the chapters that follow.

Item 1: Understand What Ruby Considers To Be True

Every programming language seems to have its own way of dealing with Boolean values. Some languages only have a single representation of true or false. Others have a confusing blend of types that are sometimes true and sometimes false. Failure to understand which values are true and which are false can lead to bugs in conditional expressions. For example, how many languages do you know where the number zero is false? What about those where zero is true?

Ruby has its own way of doing things, Boolean values included. Thankfully, the rule for figuring out if a value is true or false is pretty simple. Its different than other languages, which is the whole reason this item exists, so make sure you understand what follows. In Ruby, every value is true exceptfalse and nil.

Its worth taking a moment and thinking about what that means. While its a simple rule, it has some strange consequences when compared with other mainstream languages. In a lot of programming languages the number zero is false, with all other numbers being true. Using the rule just given for Ruby, zero is true. Thats probably one of the biggest gotchas for programmers coming to Ruby from other languages.

Another trick that Ruby plays on you if youre coming from another language is the assumption that true and false are keywords. Theyre not. In fact, theyre best described as global variables that dont follow the naming and assignment rules. What I mean by that is that they dont begin with a $ character like most global variables and they cant be used as the left-hand side of an assignment. In all other regards though, theyre global variables. See for yourself:

irb> true.class
---> TrueClass
irb> false.class
---> FalseClass

As you can see, true and false act like global objects, and like any object, you can call methods on them. (Ruby also defines TRUE and FALSE constants that reference these true and false objects.) They also come from two different classes: TrueClass and FalseClass. Neither of these classes allow you to create new objects from them, true and false are all we get. Knowing the rule Ruby uses for conditional expressions, you can see that the true object only exists for convenience. Since false and nil are the only false values, you dont need the true object in order to return a true value. Any non-false, non-nil object can do that for you.

Having two values to represent false and all others to represent true can sometimes get in your way. One common example is when you need to differentiate between false and nil. This comes up all time in objects that represent configuration information. In those objects, a false value means that something should be disabled, while a nil value means an option wasnt explicitly specified and the default value should be used instead. The easiest way to tell them apart is by using the nil? method, which is described further in Item 2. Another way is by using the == operator with false used as the left operand:

if false == x
...
end

With some languages theres a stylistic rule that says you should always use immutable constants as the left-hand side of an equality operator. Thats not why Im recommending false as the left operand to the == operator. In this case, its important for a functional reason. Placing false on the left-hand side means that Ruby parses the expression as a call to the FalseClass#== method (which comes from the Object class). We can rest safely knowing that this method only returns true if the right operand is also the false object. On the other hand, using false as the right operand may not work as expected since other classes can override the Object#== method and loosen the comparison:

irb> class Bad
def == (other)
true
end
end
irb> false == Bad.new
---> false
irb> Bad.new == false
---> true

Of course, something like this would be pretty silly. But in my experience, that means its more likely to happen. (By the way, well get more into the == operator in Item 12.)

Things to Remember

Every value is true exceptfalse and nil.

Unlike in a lot of languages, the number zero is true in Ruby.

If you need to differentiate between false and nil, either use the nil? method or use the == operator with false as the left operand.

Item 2: Treat All Objects As If They Could Be nil

Every object in a running Ruby program comes from a class that, in one way or another, inherits from the BasicObject class. Imagining how all these objects relate to one another should conjure up the familiar tree diagram with BasicObject at the root. What this means in practice is that an object of one class can be substituted for an object of another (thanks to polymorphism). Thats why we can pass an object that behaves like an arraybut is not actually an arrayto a method which expects an Array object. Ruby programmers like to call this duck typing. Instead of requiring that an object be an instance of a specific class, duck typing shifts the focus to what the object can do, in other words, interface over type. In Ruby terms, duck typing means you should prefer using the respond_to? method over the is_a? method.

In reality though, its rare to see a method inspect its arguments using respond_to? in order to make sure it supports the correct interface. Instead, we tend to just invoke methods on an object and if the object doesnt respond to a particular method, we leave it up to Ruby to raise a NoMethodError exception at runtime. On the surface, it seems like this could be a real problem for Ruby programmers. Well, just between you and me, it is. Its one of the core reasons testing is so very important. Theres nothing stopping you from accidentally passing a Time object to a method expecting a Date object. These are the sorts of mistakes we have to tease out with good tests. And thanks to testing, these types of problems can be avoided. Theres one particular kind of these polymorphic substitutions, however, that plagues even well tested applications:

undefined method `fubar' for nil:NilClass (NoMethodError)

This is what happens when you call a method on an object and it turns out to be that pesky nil object, the one and only object from the NilClass class. Errors like this tend to slip through testing only to show up in production when a user does something out of the ordinary. Another situation where this can occur is when a method returns

Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «Effective Ruby : 48 Specific Ways to Write Better Ruby»

Look at similar books to Effective Ruby : 48 Specific Ways to Write Better Ruby. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «Effective Ruby : 48 Specific Ways to Write Better Ruby»

Discussion, reviews of the book Effective Ruby : 48 Specific Ways to Write Better Ruby and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.