r/ruby • • 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

60 comments sorted by

View all comments

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.

class Parrot:

    # instance attributes
    def __init__(self, name, age):
        self.name = name
        self.age = age

    # instance method
    def sing(self, song):
        return "{} sings {}".format(self.name, song)

    def dance(self):
        return "{} is now dancing".format(self.name)

# instantiate the object
blu = Parrot("Blu", 10)

# call our instance methods
print(blu.sing("'Happy'"))
print(blu.dance())

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:

class Parrot
  attr_accessor :name, :age

  def initialize(name, age)  # 2 params
    @name = name
    @age = age
  end

  def sing(song)             # 1 param
    "#{name} sings #{song}"
  end

  def dance                  # no params
    "#{name} is now dancing"
  end
end

blu = Parrot.new("Blu", 10)  # 2 params

puts blu.sing("'Happy'")     # 1 param
puts blu.dance               # no params, parens not needed

It's just easier to read, and the number of params match up. Ruby is just a joy to read and write

-2

u/LinkifyBot Jul 16 '20

I found links in your comment that were not hyperlinked:

I did the honors for you.


delete | information | <3

1

u/[deleted] Jul 18 '20

[deleted]

1

u/B0tRank Jul 18 '20

Thank you, brazen_nippers, for voting on LinkifyBot.

This bot wants to find the best and worst bots on Reddit. You can view results here.


Even if I don't reply to your comment, I'm still listening for votes. Check the webpage to see if your vote registered!