Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Wow, Ruby is one beautiful language...

edited April 2006 in Vanilla 1.0 Help
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 big deal, 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 * 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 end

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 on 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.

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."

Comments

  • edited April 2006
    First, I'll say that I have a lot of respect for the Ruby designers. They've really tried to innovate and experiment with new language syntax and features. Sometimes, though, these can make the meaning of the code unclear to someone not well versed in ruby quirks.
    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 }
    The python method is unquestionably clearer to someone who is unfamiliar with this sort of construct. It builds on knowledge of the "for" loop, which practically every major language (ruby and python included) have.

    More importantly, the full ruby "for each" syntax is
    array.each{|x| do something}
    which is even less clear.

    Code efficiency is great. Certainly both of these options are better than the old C method of determining the number of elements in array, then setting up a generic for loop on an index variable, and using that index to address each element. Both languages take a common task, looping through the elements of an array, and let you do it with less typing.

    The true benefit, however, is clarity. The real reason that C method was bad is that it involves unneccessary information (the index of each element), and the purpose of the code is not clearly identifiable from a brief glance.

    This is where array.each falls down; it just isn't very clear what the heck it's supposed to be doing, unless I've used it before. "Each" isn't a verb, so it makes a pretty lousy name for a method. How the block of code following it relates is not obvious, and the meaning of the '|x|' is completely obtuse.

    I'm kinda rambling, and it's pretty late, so I'm going to stop now.
  • Oh no, another my Python stick is larger than your Ruby stick thread. I will have no part of this.
  • edited April 2006
    Oh dear god no; I can't stand Python. I was just pointing out that Ruby had a few, um, weird syntactical quirks that will throw off anyone who isn't expecting them.
  • I used Python for a while ... then I lost my mind to PHP. Been doing some reading on Ruby to edjumakate myself on it, and the more I see (and understand), the more I like the way it works. It's clean, generally logical, and pretty potent with minimal code. Maybe this summer while the wife and child are gone I'll get some actual time to work with it ... maybe even try Rails.
  • PanPan
    edited April 2006
    Bergamot: Ruby has a Python-like syntax for iterating over arrays to:

    ary = [1, 2] for item in ary puts item end
  • Anyone else reading 'why's poignant guide to ruby'? Dude cracks me up. I think it's the first an only time I've burst out laughing while reading a programming book.
  • edited April 2006
    Lol, it made me laugh a lot too actually. Especially the part about the dog. Though I'm not sure its the best way to go about learning programming.
  • Probably not, but it is a highly amusing way to learn about the language if you already have a programming background. I like the little fox cartoons.
This discussion has been closed.