Ruby tends to be more user-friendly than Python?

Source: Internet
Author: User

We have seen a lot of comparisons between Ruby and Python. BKJIA has also reported that Python and Ruby have their own merits. The complexity of a language can be avoided by selecting a language, but the complexity of the problem itself cannot be avoided by selecting a language.

To some extent, it is a meaningless competition between hammers and hammers.

1. String formatting

Python

 
 
  1. "%s=%s" % (k, v) 

When reading the Python string formatting, the line of sight first saw the character % s of the string, but did not know what it meant, and then looked at the variable k, then let's look at the second % s, and then look at the v behind it. The line of sight must keep beating between the string and the variable.

Ruby

 
 
  1. "#{k}=#{v}" 

While reading the Ruby string formatting, you can see where the variable is needed and where the variable is. By the way,

 
 
  1. "%s = %s" % [k,v] 

This style of code can also be used in Ruby. Ruby's philosophy is that there are more than one way to solve the problem. The choice of which one depends on the programmer's preferences.

2. ing iteration)

This is called list parsing in Python, but whatever it is, it is actually an iteration.

 
 
  1. [elem*2 for elem in li] 

Li is a list. When I read this line of code, I first saw elem * 2, but I don't know what elem is. Let's continue and check whether elem is displayed again. I have always seen in li. It turns out that elem is an element in li. By the way, what did I do to elem just now?

If the parsing of an element is not confusing, continue with the example below.

 
 
  1. ["%s=%s" % (k, v) for k, v in params.items()] 

Which part should I check first.

The following is the Ruby version:

 
 
  1. li.map {|elem| elem*2}params.map {|k, v| "#{k}=#{v}"} 

"Map the params ing), where the elements are the strings consisting of k and v Key-value pairs in the original params." I don't ensure that people without Ruby are used to this block syntax, however, I can ensure that the Code is read in a single order from left to right.

3. DSL domain language)

To give an example that is representative but simple enough in reality, I found webpy and sinatra, which are the popular concise Web frameworks in the Python and Ruby communities.

Note that webpy, or even Python, is not a Community that pursues DSL. However, the Ruby community is long on DSL, which seems unfair. However, it can be compared whether DSL is helpful for code readability.

Hello World of webpy:

 
 
  1. import web  
  2. urls = (  
  3.     '/', 'hello'  
  4. )  
  5. app = web.application(urls, globals())  
  6.    
  7. class hello:  
  8.     def GET(self):  
  9.         return 'Hello, world!'  
  10. if __name__ == "__main__":  
  11. app.run()  

I simplified the original helloworld of webpy to compare it with sinatra.

Frankly speaking, the hello world of webpy is concise enough. Compared with the huge IDE of Java EE and. net and the standards that do not know what to use, webpy makes us back to simplicity, simplicity, and simplicity. However, in terms of simplicity, Ruby's DSL culture is even more extreme. Let's look at the sinatra example:

 
 
  1. require 'sinatra'  
  2. get '/' do  
  3.   "Hello World!"  
  4. end  

Sinatra's DSL is very concise, and people may even doubt whether it is a toy. You can also read the document or user list of sinatra. Now, please believe that it is similar to webpy.

DSL is a language encapsulation that keeps complexity inside the database and exposes interfaces to programmers in the form of DSL. This is actually no different from the class and function-based APIs. However, DSL will make people forget the language they are using. Rubyists says magic.

Summary

Although Python and Ruby are both outstanding in the Dynamic Language era, their development and community styles are quite different. This is the root cause of the different concepts at the birth of the two languages: Python focuses on standardization, and there is only one method for one problem. indentation enforces constraints to facilitate cooperation among multiple people. Ruby focuses on humanization and reading convenience, there are several ways to solve this problem. Excessive magic requires you to exercise your own control skills.

The result is that people focusing on mathematics and model-oriented people like Python, and those focusing on human language like Ruby.

What do you like? You are welcome to share your message with others.

Link: http://chloerei.com/2011/01/10/714

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.