Comparison of Ruby and Python syntax

Source: Internet
Author: User
Comparison of Ruby and Python syntax
 
 
 

 

In fact, Ruby is very similar to Python, and it is much closer than most other languages, so you can use whatever you like (to be honest, although it is also nonsense ). Although there are some differences in syntax, most of them are syntax sugar. I am a little scared to list a few. (I forgot about Python. If not, let's throw it ), however, the main difference lies in the design philosophy: flexibility vs clarity. I don't think there is any difference in productivity between the two, if you are familiar with it. * Note: Only the comparison of the language itself is supported.

Recommended in this article:Ruby tutorials and tips

Comparison List of Ruby and Python

1. Ruby's case can match many things: range/array, object, regular expression, Python without case/switch, and if/else is relatively rigid

2. the indentation of python is very beautiful, although it may sometimes cause some trouble. Ruby's end is ugly, so everyone is forced to be one liner (joke)

3. I think Ruby is more oo than python. Of course, this may be because Python does not advocate the illusion of using the 'tricks 'that change the internal structure of objects.

4. Python has list comprehension, while Ruby does not :(

5. Python has a real keyword argument. Ruby uses hash for simulation. Of course, there is no difference in actual use.

6. Python's self is annoying. Ruby does not have that tedious stuff.

7. reflection. Ruby has many built-in methods, such as object. Methods. Python stores the information in a special dictionary. Similar

8. Ruby has a strong block function. Python Lambda can only return a single value.

9. Ruby's open class has been declared to be a remote broadcast. You can use a pattern like 2. Days. ago. Python does not seem to be able to directly modify the built-in class.

10. Python needs to use @ classmethod to modify the Declaration class method. Ruby is built-in.

11. Ruby has a list method, which is customized for objects separately. Python does not know whether similar concepts exist.

12. Ruby has the method_missing mechanism. python can use _ getattr _ to intercept undefined methods (from Qiezi)

13. Ruby uses single inheritance + Mixin, and python uses multiple inheritance, but Python also has Mixin

14. Ruby has the ATTR _ * series syntax helper, saving you the trouble of writing a bunch of setter/getter. The Python property method still has to write setter/getter by yourself.

15. Both Ruby and Python use duck typing, but Python also has an explicit interface mechanism (is zope3 incorporated into the kernel ?)

16. Ruby's function call brackets can be saved, with a few mouse clicks on the keyboard. Python returns a reference of the function without parentheses or parameters by default.

17. I don't know how much the python meta programming can do, so I have to wait for Daniel to talk about it. I just think that pythoner is not commonly used.ProgramThe logic is obscure.

18. Ruby inherits some ugly things from Perl, such as many predefined $ X constants.

19. Ruby built-in Regular Expression for convenience

20. Ruby's yield is used to call block. The yield of python is used to input and output values to generator.

21. The Python library makes me feel that the naming rules are a bit inconsistent. Some methods use the "Snail ke_case" method, while others use the "camelcase" method. Maybe there are too many libraries with historical issues left behind.

22. the three quotation marks of Python are very beautiful, and Ruby's <-XX... XX is too ugly. You can also use % q {...} package multi-line text (from Qiezi)

23. In Ruby's class library design, you like to add aliases to methods for easy memory.

In addition, the ruby official website also provides some basic comparisons between Ruby and python.

 

Publisher: 51cto Date: 13:45:00

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.