You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With more freedom and less syntactical rules, many Rubyists believe that Ruby is a much more elegant language - and it is. But you can also often see messy code (especially from beginners) that can be difficult for other developers to read. For example, you can put multiple statements on one line. This can look good and be readable, depending on how it's coded - or it can be a mess.
86
+
With more freedom and less syntactical rules, many Rubyists believe that Ruby is a much more elegant language - and it is. Also, while Python focuses on simplicity, Ruby focuses on making the programmer happy, while this tends to be true and people can make truly beautiful programs, you can also often see messy code (especially from beginners) that can be difficult for other developers to read( or even themselves in a not so distant future). For example, you can put multiple statements on one line. This can look good and be readable, depending on how it's coded - or it can be a mess.
87
+
88
+
###The principle of least astonishment
89
+
90
+
One princpiple very present in the design of Ruby is the principle of least astonishment, it's basically about the language working the way you think it should, thinks like using a function with a strange name will leave you astonished, and will make you look in the documentation.
91
+
In order to behave like this Ruby tends to do things like having the same functions with different names,however some people would argue this is confusing for beginners. Also, Python , although not having this principle in mind when making the language, also behaves quite like this by make pretty much everything work in a standard way, however one could say this only benefits advanced Python users.
92
+
87
93
88
94
Let's compare some code. The following snippets of code are for solving the Fibonacci sequence:
89
95
90
96
*Ruby:*
91
97
92
98
```ruby
93
99
deffib(n)
94
-
n <=2?1 : fib(n-2) + fib(n-1)
100
+
return n if n <2
101
+
fib(n-1) + fib(n-2)
95
102
end
103
+
alias:fibonacci:fib
96
104
```
97
105
98
106
*Python:*
@@ -105,7 +113,7 @@ def fib(n):
105
113
return fib(n-1) + fib(n-2)
106
114
```
107
115
108
-
Although you can write this code in many ways, both of these methods are *true* to the language. In other words, the Ruby example is very Ruby-ish while the Python example is very Pythonic. Can you read the Ruby code? It may be more elegant but it's a bit harder to read. Meanwhile, it's easy to follow the Python code, right? You of course can write code anyway you want. It's advisable to write Ruby code, when beginning, in a more Pythonic way - which simply means making it more readable:
116
+
Although you can write this code in many ways, both of these methods are *true* to the language, Ruby code demonstrates the practice of having multiple names for the same function (fib and fibonacci are the same). In other words, the Ruby example is very Ruby-ish while the Python example is very Pythonic. Can you read the Ruby code? It may be more elegant but it's a bit harder to read. Meanwhile, it's easy to follow the Python code, right? You of course can write code anyway you want. It's advisable to write Ruby code, when beginning, in a more Pythonic way - which simply means making it more readable:
109
117
110
118
```ruby
111
119
deffib(n)
@@ -138,15 +146,6 @@ Keep in mind that in many cases with Python there are still a number of ways to
138
146
139
147
The difference is that there is one right way of doing this given the situation. The latter two probably are not the best to do since you have to use an extra library and list comprehensions can often be hard to read, respectively. The second example is the most readable, so that should probably be used in most situations.
140
148
141
-
Oh - And you can also write the above Fibonacci code in Python in a more Ruby-like manner:
142
-
143
-
```python
144
-
deffib(n):
145
-
return n if n <2else fib(n-1) + fib(n-2)
146
-
```
147
-
148
-
(Some would probably argue that this is more Pythonic than the above Python example.)
149
-
150
149
### More differences
151
150
152
151
As you can imagine, there are many more differences that just the syntax and philosophies of the two languages. Let's quickly look at some examples.
@@ -175,7 +174,7 @@ In the first example (Python), we are importing the `Twitter()` class from the `
175
174
176
175
#### Programming Paradigms
177
176
178
-
Again, you can use the same paradigms in both languages (procedural, functional, object oriented ...). When it comes to object oriented programming, Ruby used to have the upper hand, as it was built specifically for object orientation. That said, Python has moved more towards being a true object orientated language over the last few years. This is a toss-up. Neither language has the upper hand, in other words.
177
+
Again, you can use the same paradigms in both languages (procedural, functional, object oriented ...). When it comes to object oriented programming, Ruby used to have the upper hand, as it was built specifically for object orientation. That said, Python has moved more towards being a true object orientated language over the last few years. However, Ruby has one advantage, Ruby can add methods to existing classes, Python by default can't do this (although it's possible with the use of a external library)
0 commit comments