Ruby is probably a teeny bit more cleanly designed (none of that goofy "self" business, no multiple inheritance), but Python's syntax is a bit cleaner and encourages good coding practices (I love docstrings)Thank you!
people.find_all { |person| person.age > 10 }.sort_by { |person| person.age }class Foo
def process(self, bar, baz, boo)
self.do_stuff # self gets passed in automatically, but we had to declare it as a parameter
end
versus Ruby, where self is implicit at all times. class Foo
def process(bar, baz, boo)
self.do_stuff # calls self on this instance of Foo, available implicitly
end
I dislike the self thing, because it means I repeat myself a ton and it seems something the intrepeter should be able to figure out on its own. I'm sure if I worked in Python everyday it would eventually disappear, just like @'s in Ruby.
# Time extensions available in Rails via ActiveSupport, but you can do it yourself due to open classes
20.years.from_now
Time.now + 2.days + 10.hours + 15.minutes
# blocks, aka closures
(1..10).each { |num| puts num * 2 } # prints "2 4 6 8 "... etc
# find all managers from the employees collection
managers = employees.select { |employee| employee.manager? }
# validating data in Rails
class FrontPagePost < ActiveRecord::Base
validates_uniqueness_of :title
validates_prescence_of :title, :link, :link_text
validates_length_of :content, :minimum => 0, :maximum => 500
end5.times print "Hello"It should be obvious what that does; less obvious but still cool is that the integer 5 is a full-on object, and 'times' is one of its functions. From a readability/elegance perspective alone that's a win. I'm in no position to talk about computational advantages and so forth, but it makes the writing easier and more fun. And people forget how important those things are. I've never enjoyed coding as much as I've enjoyed coding Ruby. It was like learning Scheme. That's my strongest recommendation. But as for learning, I'm unsure. Ditto the above: try _why's Ruby-online tutorial, play around in irb.
class HelloWorld {
public static void main(String[] args) {
System.out.println("hello from java!");
}
}Ruby:
class HelloWorld
def helloWorld
puts "Hello from Ruby"
end
end
# or even just:
puts "hello from ruby, no need for a method or class
I believe Python would be very similiar, where you can just define a top level function in any old file.Class Enum<E extends Enum<E>>Explain that to a first year student. Also worth reading: Generics Considered Harmful. Or go read Bruce Eckel on generics, the author of _Thinking in Java_. His experience with trying to write about generics for 5 is illuminating.
One very strange thing about python is that "white space matters" Different levels of nesting are demarcated by different levels of indentation, rather then braces. This is vastly different from almost every other programming language, and as someone who's programmed in at least 15 different languages the idea bugs the hell out of me. And ruby is definitely trendier. People seem to love the Rails thing for web design.
One thing that sucks about Ruby is that it's character handling is all fucked up. Remember how metafilter used to have trouble handling foreign characters? That was a problem with some of the underlying code, rather then just "working" matt had to do some crazy hack to fix it: or upgrade the JDBC driver, I don't remember. If you're not worried about internationalization it won't be a problem for you.
But don't get stuck in a rut of doing things only in one programming language. You should actually learn both if they both interest you.
posted by delmoi at 7:01 PM on August 31, 2006