Skip to content

Commit ad5a1f4

Browse files
committed
Merge pull request mjhea0#7 from sheosi/master
Improved Ruby information
2 parents ab853b0 + 9b4090e commit ad5a1f4

1 file changed

Lines changed: 12 additions & 13 deletions

File tree

readme.md

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,16 +83,24 @@ While Python values one main way of solving a problem, Ruby - influenced by Perl
8383

8484
Source: [http://www.artima.com/intv/rubyP.html](http://www.artima.com/intv/rubyP.html)
8585

86-
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+
8793

8894
Let's compare some code. The following snippets of code are for solving the Fibonacci sequence:
8995

9096
*Ruby:*
9197

9298
```ruby
9399
def fib(n)
94-
n <= 2 ? 1 : fib(n-2) + fib(n-1)
100+
return n if n < 2
101+
fib(n-1) + fib(n-2)
95102
end
103+
alias :fibonacci :fib
96104
```
97105

98106
*Python:*
@@ -105,7 +113,7 @@ def fib(n):
105113
return fib(n-1) + fib(n-2)
106114
```
107115

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:
109117

110118
```ruby
111119
def fib(n)
@@ -138,15 +146,6 @@ Keep in mind that in many cases with Python there are still a number of ways to
138146

139147
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.
140148

141-
Oh - And you can also write the above Fibonacci code in Python in a more Ruby-like manner:
142-
143-
```python
144-
def fib(n):
145-
return n if n < 2 else fib(n-1) + fib(n-2)
146-
```
147-
148-
(Some would probably argue that this is more Pythonic than the above Python example.)
149-
150149
### More differences
151150

152151
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 `
175174

176175
#### Programming Paradigms
177176

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)
179178

180179
#### Performance
181180

0 commit comments

Comments
 (0)