@@ -28,24 +28,6 @@ By the way, if you're into Rails you might want to check out the
2828complementary
2929[ Ruby on Rails 3 Style Guide] ( https://github.com/bbatsov/rails-style-guide ) .
3030
31- ## Table of Contents
32-
33- * [ The Ruby Style Guide] ( #guide )
34- * [ Source Code Layout] ( #layout )
35- * [ Syntax] ( #syntax )
36- * [ Naming] ( #naming )
37- * [ Comments] ( #comments )
38- * [ Annotations] ( #annotations )
39- * [ Classes] ( #classes )
40- * [ Exceptions] ( #exceptions )
41- * [ Collections] ( #collections )
42- * [ Strings] ( #strings )
43- * [ Percent Literals] ( #literals )
44- * [ Miscellaneous] ( #misc )
45- * [ Design] ( #design )
46- * [ Contributing] ( #contributing )
47- * [ Spread the word] ( #spreadtheword )
48-
4931# The Ruby Style Guide
5032
5133This Ruby style guide recommends best practices so that real-world Ruby
@@ -73,7 +55,6 @@ mind for now.
7355You can generate a PDF or an HTML copy of this guide using
7456[ Transmuter] ( https://github.com/TechnoGate/transmuter ) .
7557
76- <a name =" layout " >
7758## Source Code Layout
7859
7960> Nearly everybody is convinced that every style but their own is
@@ -181,7 +162,6 @@ You can generate a PDF or an HTML copy of this guide using
181162* Keep lines fewer than 80 characters.
182163* Avoid trailing whitespace.
183164
184- <a name="syntax"/>
185165## Syntax
186166
187167* Use `def` with parentheses when there are arguments. Omit the
@@ -506,9 +486,31 @@ would happen if the current value happened to be `false`.)
506486`f((3 + 2) + 1)`.
507487
508488* Always run the Ruby interpreter with the `-w` option so it will warn
509- you if you forget either of the rules above!
489+ you if you forget either of the rules above!
490+
491+ * When the keys of your hash are symbols use the Ruby 1.9 hash literal
492+ syntax.
493+
494+ ```Ruby
495+ # bad
496+ hash = { :one => 1, :two => 2 }
497+
498+ # good
499+ hash = { one: 1, two: 2 }
500+ ```
501+
502+ * Use the new lambda literal syntax.
503+
504+ ```Ruby
505+ # bad
506+ lambda = lambda { |a, b| a + b }
507+ lambda.call(1, 2)
508+
509+ # good
510+ lambda = ->(a, b) { a + b }
511+ lambda.(1, 2)
512+ ```
510513
511- <a name="naming"/>
512514## Naming
513515
514516> The only real difficulties in programming are cache invalidation and
@@ -538,7 +540,6 @@ would happen if the current value happened to be `false`.)
538540 *find_all*, `size` over *length*. This is not a hard requirement; if the
539541 use of the alias enhances readability, it' s ok to use it.
540542
541- < a name= " comments" / >
542543# # Comments
543544
544545> Good code is its own best documentation. As you' re about to add a
@@ -562,7 +563,6 @@ would happen if the current value happened to be `false`.)
562563* Avoid writing comments to explain bad code. Refactor the code to
563564 make it self-explanatory. (Do or do not - there is no try.)
564565
565- <a name=" annotations" />
566566## Annotations
567567
568568* Annotations should usually be written on the line immediately above
@@ -603,9 +603,13 @@ would happen if the current value happened to be `false`.)
603603* Use other custom annotation keywords if it feels appropriate, but be
604604 sure to document them in your project's `README` or similar.
605605
606- <a name=" classes" />
607606## Classes
608607
608+ * When designing class hierarchies make sure that they conform to the
609+ [Liskov Substitution Principle](http://en.wikipedia.org/wiki/Liskov_substitution_principle).
610+ * Try to make your classes as
611+ [SOLID](http://en.wikipedia.org/wiki/SOLID_(object-oriented_design))
612+ as possible.
609613* Always supply a proper `to_s` method.
610614
611615 ```Ruby
@@ -678,14 +682,12 @@ in *Ruby* now, not in *Python*.
678682 end
679683 ```
680684
681- < a name= " exceptions" / >
682685# # Exceptions
683686
684687* Don ' t suppress exceptions.
685688* Don' t use exceptions for flow of control.
686689* Avoid rescuing the ` Exception` class .
687690
688- < a name= " collections" / >
689691# # Collections
690692
691693* It ' s ok to use arrays as sets for a small number of elements.
@@ -699,7 +701,6 @@ strings.
699701* Rely on the fact that hashes in 1.9 are ordered.
700702* Never modify a collection while traversing it.
701703
702- <a name="strings"/>
703704## Strings
704705
705706* Prefer string interpolation instead of string concatenation:
@@ -761,7 +762,6 @@ strings.
761762 end
762763 ```
763764
764- <a name="literals"/>
765765## Percent Literals
766766
767767* Use `%w` freely.
@@ -808,7 +808,11 @@ strings.
808808
809809* Prefer `()` as delimiters for all `%` literals.
810810
811- <a name="misc"/>
811+ ## Metaprogramming
812+
813+ * Do not mess around in core classes when writing libraries. (Do not monkey
814+ patch them.)
815+
812816## Misc
813817
814818* Write `ruby -w` safe code.
@@ -835,39 +839,14 @@ strings.
835839
836840* Avoid `alias` when `alias_method` will do.
837841* Use `OptionParser` for parsing complex command line options and
838- `ruby -s` for trivial command line options.
839- * Write for Ruby 1.9. Don' t use legacy Ruby 1.8 constructs.
840- * Use the new JavaScript literal hash syntax.
841- * Use the new lambda syntax.
842- * Methods like ` inject` now accept method names as arguments.
843-
844- ` ` ` Ruby
845- [1, 2, 3].inject(:+)
846- ` ` `
847-
848- * Avoid needless metaprogramming.
849-
850- < a name= " design" / >
851- # # Design
852-
842+ `ruby -s` for trivial command line options.
853843* Code in a functional way, avoiding mutation when that makes sense.
844+ * Avoid needless metaprogramming.
854845* Do not mutate arguments unless that is the purpose of the method.
855- * Do not mess around in core classes when writing libraries. (Do not monkey
856- patch them.)
857- * [Do not program
858- defensively.](http: / /www.erlang.se/ doc/ programming_rules.shtml# HDR11)
859- * Keep the code simple and subjective. Each method should have a single,
860- well- defined responsibility.
861846* Avoid more than three levels of block nesting.
862- * Don ' t overdesign. Overly complex solutions tend to be brittle and hard to
863- maintain.
864- * Don' t underdesign. A solution to a problem should be as simple as
865- possible, but no simpler than that. Poor initial design can lead to a lot
866- of problems in the future.
867847* Be consistent. In an ideal world, be consistent with these guidelines.
868848* Use common sense.
869849
870- < a name= " contributing" / >
871850# Contributing
872851
873852Nothing written in this guide is set in stone. It' s my desire to work
@@ -878,7 +857,6 @@ community.
878857Feel free to open tickets or send pull requests with improvements. Thanks in
879858advance for your help!
880859
881- <a name="spreadtheword"/>
882860# Spread the Word
883861
884862A community- driven style guide is of little use to a community that
0 commit comments