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...
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,
Looks like a basic statement. Its not, it is a method of the form:
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:
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:
Cuts the newline off of the string in place, however:
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:
While Python makes you do something not as simple, more along the lines of:
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:
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:
Where as the "way" of doing things in Ruby is more along the lines of:
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."
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."
0
This discussion has been closed.
Comments
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.
ary = [1, 2] for item in ary puts item end