DEV Community

Discussion on: What's the difference? Ruby vs. Python

Collapse
 
questionmarkexclamationpoint profile image
questionmarkexclamationpoint

Ruby has Complex numbers. Ruby has sets (they also have a nice feature of being ordered in the same way you originally inserted). Instead of a tuple in Ruby, you can use #freeze on an array. Ruby also has a fraction type. Ruby also has a range type (which could replace nums in your example with (1..5)). Also worth noting that Ruby uses a big integer implementation, so there is effectively no max integer.

Although there are many ways to do the same thing, the more Ruby-like way to form those loops would be nums.each and 7.times. When I first started using Ruby I found myself trying to write things the way I have been taught in other languages. As I learn more and more about the various ways to do things in Ruby I am amazed at how readable things can be if the language gives you the right facilities. while i < 7 may be perfectly readable to you as someone who has written code before, but if you leave your preconceptions at the door I think you'll find that 7.times is more understandable. The same is true, I find, for so many things in Ruby.

Another thing of note is that in addition to if you can use unless, and in addition to while you can use until. You can also put these constructs after a statement, on the same line, to simplify and make things more readable, for instance: return true if i < 3, or x += 1 until x > 10 (kind of a silly example).

One thing I think you should highlight about Ruby is the stark difference between it and Python when it comes to lambdas. Ruby has the upper hand there.

Additionally, Ruby allows you to monkey patch your own code into existing classes. Wish Integer had a #factorial method? Monkey patch it.

I learned Python first, and then Ruby. When I have to write Python code now I feel as though I am bashing rocks together to make fire. When I use Ruby I feel like I am programming.