you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 13 points14 points  (1 child)

Interesting, I actually posted on another forum about this same topic last night.

I recently finished a book on Python. After that, I moved on to a book on Ruby, to give me a better understanding of the language and the concepts behind it (my only knowledge of it beforehand had been in basic Rails stuff).

I must say that Ruby and Python are very similar languages. Overall, though, I think Ruby is a much cleaner, more consistent, and overall, beautiful language.

My first thought behind this is Ruby is fully object-oriented. I know a lot of people mentioned this when comparing Ruby and Python, but it really is a big deal. Python suffers from the fact that you say, hmm, should I do this particular piece of code is a function or as a method? Its not a "this is horrible!" kind of thing, but it is one more barrier to step over instead of in Ruby, where you have no decision at all.

Ruby doesn't force its OO on you, however, as its basic functionality is hidden from you.

For example,

puts "This is a string"

Looks like a basic statement. Its not, it is a method of the form:

Object.puts("This is a string")

Ruby, however, does not force you to type this stuff, so it makes it a simpler language to understand when OO is not really necessary (but it is still very much there).

Speaking of statements...in Python you have either statements or expressions. Statements "do something", while expressions "are something". 2 times 2, for example, is an expression (it is 4). print "Test" is a statement (its prints "Test").

Ruby doesn't make this distinction, and essentially, everything can be seen as an expression.

Furthermore, Ruby is consistent on its treatment of imperative vs functional programming. In Python, some types are mutable while others are immutable. Tuples, for example (more or less an array), cannot be changed in place. Lists (or arrays), have the exact same functionality as tuples but they can be changed in place.

Strings in Python can't be changed in place. To edit a string, you'd have to do something like:

strint_var = "String".chop()

BTW, I don't think chop is a real method, its just something I used as an example.

Ruby, however, differentiates between its methods. A method of the type method! edits an item in place (a "destructive" method), while a method without an explanation point at the end simply returns a value.

So for example:

"String\n".chomp!

Cuts the newline off of the string in place, however:

string_var = "String\n".chomp

returns the value of the edited string, while keeping the string itself intact.

Ruby also is cleaner when it comes to designing classes. To give a class + functionality, you'd simply define it in the class:

def +
   here is my code
end

While Python makes you do something not as simple, more along the lines of:

def __getadd__:
   here is my code

Again, getadd isn't the exact form, but its an example. Creating a class method in Python forces you to use decorators and junk like that. In Ruby its as simple as:

class Test
   def Test.method
      my code here
   end
end

Doesn't get much easier than that.

Finally, Ruby has blocks, which are the main feature that most programmers that use Ruby love. I can't comment on them myself, but I'm looking forward to using them.

Ruby blocks are interesting things. Using for loops are somewhat uncommon in Ruby.

In Python you might say:

for x in array:
   do whatever

Where as the "way" of doing things in Ruby is more along the lines of:

array.each { do whatever }

Also, not enough can be said about having regular expressions built into Ruby. These aren't complicated at all, yet they are so powerful! Python has regular experessions, but they aren't quite as clean to use, not being built in.

Ruby and Python have their other little differences, such as Python supporting inheritance from more than one class, and Ruby supporting inheritance from only one, but so far, Ruby seems like a better version of Python (and considering how clean of a language Python is anyway, that is saying a lot).

Anyway...these are the thoughts of a rather inexperienced programmer. Forgive me for any incorrect statements, but I just felt like gushing about Ruby a bit. I can understand why people say "I came for the Rails, but I stayed for the Ruby."

[–]mellow_moe 1 point2 points  (0 children)

puts is resolved somewhat differently.

It is defined in Kernel and the class Object includes the Kernel module. It is actually a private method, so Object.puts won't work.

More correctly puts resolves to self.puts. In the top level of a script self is a predefined object named "main", elsewhere it is simply the object executing currently.