GitHub advocates a summary of Ruby coding styles, while github advocates ruby code.

Source: Internet
Author: User

GitHub advocates a summary of Ruby coding styles, while github advocates ruby code.

Source code layout:
1. All source files are UTF-8 encoded
2. indent with two spaces
3. Use Unix-style line breaks (\ n) and windows (\ r \ n ). You can use git config -- global core. autocrlf true to prevent the generation of windows-style line breaks.
4. After ',', ';', add spaces before and after the operator (except for exponential operations) and '{', '}' to improve code readability.
5. Do not add spaces after the '(', ')', '[', ']' symbols.
6. Case and When are at the same code level.
7. Separate Two def with one blank line
8. If multiple parameters in a method are too long, perform the following operations:

def send_mail(source)  Mailer.deliver(to: 'bob@example.com',          from: 'us@example.com',          subject: 'Important message',          body: source.text) end 

9. Use the API documentation of the Rdoc production system. Do not enter blank lines between comments and def.
10. Each line cannot exceed 80
11. Do not enter any blank characters at the end of each line.

Syntax:
1. brackets are omitted for a method without parameters, and brackets are used for parameters.
2. Do not use for unless necessary, but use each for Loop
3. Do not use then
4. How to use a Trielement operation? : Replace if and else.
5. Do not use it inside if else? : Operator
6. Use when X then... because when x is deleted in ruby1.9.
7. Use & | to perform boolean operations, and use and or for process control.
8. unless and else should not be used together
9. Unless the judgment condition involves the assignment operation, it does not need to be included.
10. Use the multiline statement block {} to include,
11. Do not use a return statement that is not required.
12. Do not use '\' to connect two lines of code.
13. Use | = to initialize the variable, but it cannot be used to initialize the boolean variable.
14. Do not use perl-style variable names, such as $1-9.
15. When running Ruby, add "-w" To remind us of poor code.
16. Use the Ruby1.9 syntax to write lambda and hash

Naming rules:
1. Use lower case + "_" to name variables and Methods
2. Use uppercase letters to name the Module and Class
3. Use full capital + "_" to name Constants
4. Add "?" to the method whose return value is boolean. Suffix
5. Add "!" to some potentially risky methods. Suffix, such as exit, self modification, or variable.

Note:
1. There is only one comment, and the code is a comment,

Class:
1. In accordance with the liskov principle, child classes can replace parent classes.
2. Try to make the class SOLID (Single responsibility, Open-closed, Liskov substitution, Interface segregation and Dependency inversion)
3. Write a to_s method for each class to view the class status.
4. Use the attr family Method for Class Attribute Access Control
5. consider adding new factory methods for meaningful instance initialization.
6. Use DuckTyping instead of inheritance. Because of the Dynamic Language features, there is no need for polymorphism.
7. Avoid using @ and global variables,
8. Access Control operators should be properly used based on access conditions.
9. Use self to define the singleton method instead of the class name.

Exception:
1. Do not let go of exceptions
2. Do not use exceptions for Process Control
3. Do not capture exceptions. The Exception base class.
4. Exception sorting based on coverage of exception types
5. Place all external resources in the exception capture module.
6. Prioritize database exceptions rather than self-created exceptions.

Set:
1. Use % w to create a string array first
2. Create an array as needed
3. Use Set to remove repeated elements in the List
4. Do not use a mutable object as the Hash key instead of a String using Symbol.
5. Do not change a list while traversing it.

Strings:
1. "# {String }#{ string}" is better than String + String
2. If the String in the form of # {} is not used, ''is used to indicate
3. When connecting instance variables, do not use {}
4. Use <instead of + for String concatenation

Regular Expression:
1. Use a naming group instead of $1-9 to facilitate tracking.
2. '^', '$' indicates matching the entire line. '\ A',' \ Z' should be used to match the entire string'
3. Use the 'X' modifier to modify complex regex statements to improve readability, but pay attention to the space removal problem.

% Syntax:
1. Use % w more
2. Use % () When strings are embedded expressions ()
3. Use % r when multiple/
4. Do not use % q, % Q, % x, % W, % s.
5. Use () as the separator first after %

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.