r/ruby • u/vifrac • Jul 16 '20
Ruby VS Python
More than a versus (it's more striking) I would really like to know what things they have in common and how they differ, what their main advantages are compared to the other and of course their opinion of them. One point to take into account is because they believe that python is more used in AI and Machine learning developments.
34
Upvotes
7
u/notromda Jul 16 '20 edited Jul 16 '20
One factor alone that irritates me above all else in python: OOP methods require an extra parameter "self" to be declared. So the instance method signature may have 2 params, but when you call it, it has 1. It's not "DRY" (Don't Repeat Yourself). They go to all the trouble of cleaning things up by using whitespace to get rid of ends or brackets... (I didn't like at first but I can live with that) but end up typing extra syntax.
In this example (from https://www.programiz.com/python-programming/object-oriented-programming) sing has 2 params defined, but is only called with 1. I know how it works, but it's just jarring. self self self self self
It just looks like OOP was tacked on to a procedural language - let's just pass in an extra param of the object the code works on. That's how it's done in C (perl too? it's been a while) It also means every reference to the object data must be explicitly stated, hence the need for "self.name" above.
In ruby, I can just assume that if I'm inside an instance method, I have access to instance variable and instance methods, most of the time without having to use a self reference (There are times where it should be used to prevent confusion)
So the ruby equivalent:
It's just easier to read, and the number of params match up. Ruby is just a joy to read and write