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
- "%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
- "#{k}=#{v}"
While reading the Ruby string formatting, you can see where the variable is needed and where the variable is. By the way,
- "%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.
- [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.
- ["%s=%s" % (k, v) for k, v in params.items()]
Which part should I check first.
The following is the Ruby version:
- 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:
- import web
- urls = (
- '/', 'hello'
- )
- app = web.application(urls, globals())
-
- class hello:
- def GET(self):
- return 'Hello, world!'
- if __name__ == "__main__":
- 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:
- require 'sinatra'
- get '/' do
- "Hello World!"
- 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