Ruby code style you should know

Source: Internet
Author: User

I. Spaces and tabs
Use space indentation (95.364%)
Use Tab indent (4.636%)
Based on 1,001,987 submissions
Ii. widest line
80 characters (92.618% characters)
120 characters (5.893% characters)
150 characters (1.489% characters)
Based on 1,057,374 submissions
3. Whether to use spaces around operators, colons, and curly brackets after commas and semicolons
Do not use spaces (73.366%)
Copy codeThe Code is as follows:
Sum = 1 + 2
A, B = 1, 2
1> 2? True: false; puts 'Hi'
[1, 2, 3]. each {| e | puts e}

Use spaces (26.634%)
Copy codeThe Code is as follows:
Sum = 1 + 2
A, B = 1, 2
1> 2? True: false; puts 'Hi'
[1, 2, 3]. each {| e | puts e}
Based on 911,301 submissions
Iv. Default Parameters
Front and back spaces (59.151%)
Copy codeThe Code is as follows:
Def some_method (arg1 =: default, arg2 = nil, arg3 = [])
# Do something...
End
There is no space before and after or only one space (40.849%)
Copy codeThe Code is as follows:
Def some_method (arg1 =: default, arg2 = nil, arg3 = [])
# Do something...
End
Based on 19,213 submissions
5. Long numbers
Do not use underline (97.984%)
Copy codeThe Code is as follows: num = 1000000
Underline (2.016%)
Copy codeCode: num = 0000000_000
Based on 133,530 submissions
6. Whether parentheses are omitted when no parameter is set
Omit brackets (98.556%)
Copy codeThe Code is as follows:
Def some_method
# Do something...
End
Parentheses (1.444%) are not omitted)
Copy codeThe Code is as follows:
Def some_method ()
# Do something...
End
Based on 267,404 submissions
7. Use parentheses around parameters when defining functions
Omitted (62.867%)
Copy codeThe Code is as follows:
Def some_method arg1, arg2
# Do something...
End
Not omitted (37.133%)
Copy codeThe Code is as follows:
Def some_method (arg1, arg2)
# Do something...
End
Based on 365,121 submissions

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.