r/programming • • Aug 30 '08

Tour De Babel -- Rant about programming languages (at Amazon)

http://steve.yegge.googlepages.com/tour-de-babel
11 Upvotes

122 comments sorted by

View all comments

Show parent comment

9

u/jmhodges Aug 31 '08

I'm going to disagree with Leonidas and say that "[w]hen you learn Ruby or Python you don't need to learn the other one" is a false statement. I came to ruby from python thinking ruby was just another python. I was very, very wrong.

8

u/[deleted] Aug 31 '08

As a former Python programmer, what stuff in Ruby wins you over? I take it it's not just a syntax preference.

12

u/martincmartin Aug 31 '08

As someone who has used both (and also contributed to Groovy), I think the biggest difference is that Ruby has real anonymous functions, that can have an arbitrary number of statements/expressions in them. It makes dealing with any sort of collection much easier. Python's list comprehensions seem timid in comparison: they can only do "collect" and "grep," with single expressions, and the syntax for combining them is different than the rest of the language.

In Ruby, you can write a function which takes a file name and an anonymous function as arguments, then opens the file, passes it to the anon function, then closes the file in a "finally" block. It's much nicer than having to have explicit finally blocks.

0

u/[deleted] Aug 31 '08

[deleted] — view deleted comment

2

u/Niten Aug 31 '08 edited Aug 31 '08

The difference is mostly syntactic sugar. Python doesn't have multi-line lambdas, so you have to name your function before you pass it in to file_funct:

def funct(myfile):
    do_stuff_to(myfile)
    do_other_stuff_to(myfile)

file_funct(filename, funct)

In Ruby, on the other hand, you can send file_method an anonymous block, rather than define and then pass it a named method:

file_method(filename) { |myfile|
    do_stuff_to(myfile)
    do_other_stuff_to(myfile)
}

This makes things a bit cleaner, especially since Ruby has such nice syntax for passing anonymous blocks to methods. But it doesn't really change what you are able to accomplish in your code.

1

u/Smallpaul Sep 01 '08

In Python that would just be:

with file_method(filename) as myfile:
   do_stuff_to(myfile)
   do_other_stuff_to(myfile)

1

u/almkglor Sep 01 '08 edited Sep 01 '08

Out of curiousity, how generic is "with" in Python?

Suppose I have a unwinding process for a function (scheme):

(let ((foo #f))
  (dynamic-wind
    (lambda () (set! foo (constructor)))
    (lambda () (do-stuff-to foo)
               (do-other-stuff-to foo))
    (lambda () (destructor foo))))

Is there a generalized syntax which allows for such a constructor/deconstructor style? (in the case of languages which support full continuations, this also performs cleanup upon exiting via an external continuation; I believe the Ruby finally block also handles this properly)

→ More replies (2)

6

u/jmhodges Aug 31 '08

mcantelon, that ^ pretty much. It sounds weird, because it is weird when you first run into it. It's a bit different than passing functions around (even anonymous ones in functional languages) because the syntax to do this.. feels different.. cleaner. It's hard to explain, but when you see it and grok it, its a very nice way of seeing a problem in a simpler, easier to understand way.

Oh, and everything is an object and really, really an object and holy christ is it awesome not to have to pass self around everywhere. And modules rock.

I should note that I still like python. Ruby just has some things that are nifty that python doesn't.

5

u/ubernostrum Aug 31 '08 edited Aug 31 '08

Oh, and everything is an object and really, really an object

Just like Python, you mean?

Edit: and... what? I get downmodded for pointing out that the same is true in Python? Show me something in Python that isn't an object, then.

3

u/shen Aug 31 '08

I know that Python uses objects for everything, but the difference here is that Ruby consistently uses object-oriented syntax, while Python doesn't (list comprehensions, blocks instead of loops, a few sticking-out functions like len, for example).

2

u/ubernostrum Aug 31 '08 edited Aug 31 '08

And that's not the same thing at all. There's a big difference between saying, say, that Ruby uses message-passing syntax everywhere and Python doesn't use method-calling syntax everywhere (almost true about Ruby, true about Python -- there are situations where Ruby doesn't use consistent syntax) and saying, say, that in Ruby everything is an object and implying that in Python not everything is an object (which is false).

3

u/shen Aug 31 '08

I know that. I'm just saying that people generally mean the former when they say the latter.

→ More replies (1)

3

u/[deleted] Aug 31 '08

print

1

u/Leonidas_from_XIV Aug 31 '08

Won't be in Python 3.0, your complaint is ages old and has been heard by the developers.

3

u/[deleted] Aug 31 '08

I was answering a question, not complaining.

1

u/Smallpaul Sep 01 '08

Keywords are not objects in Python. Nor are they are in Ruby. Ruby has more keywords than Python.

3

u/[deleted] Aug 31 '08 edited Aug 31 '08

+ ?

5

u/ubernostrum Aug 31 '08 edited Aug 31 '08

That operator delegates to the __add()__ method of its left-hand operand; a + b is literally the same as a.__add__(b), just with nicer syntax. And unless I'm mistaken, Ruby has no standalone, built-in object named "+" (instead using it as syntax to delegate to a method call, just like Python), so if this is an argument against Python it's one against Ruby as well.

1

u/[deleted] Aug 31 '08 edited Aug 31 '08

I know it delegates to the __add()__ method of its left-hand operand. It's not an object though (or is it? to be honest, I don't know python internals well enough to know. It seems like it's just a symbol-table entry to me)

In the case that I am mistaken, what about def, or a comment? There's tons of stuff in python that aren't objects. If keywords and comments are objects in python, why can't I treat them like objects? Or can I?

3

u/ubernostrum Aug 31 '08

OK, then, as long as you're willing to make exactly the same critique of Ruby (i.e., that some syntactic elements are not in fact objects), I'm OK with that.

Typically what I think when I hear "everything is an object", though, is that everything a program will be accessing or manipulating at runtime is an object, not that every byte in the source code file ultimately corresponds to some object in the program at runtime, and I think that's much more in line with what people mean when they use that phrase (because, honestly, by the line of reasoning you're taking you wouldn't even be able to say "everything" in Smalltalk is an object, which would be absurd).

→ More replies (1)
→ More replies (2)
→ More replies (1)

2

u/Tuna-Fish2 Aug 31 '08 edited Aug 31 '08

python 3:

with open("file.txt") as file: doSomething(file)

or locking:

with myLock: doSomething()

http://docs.python.org/dev/3.0/reference/compound_stmts.html#the-with-statement

5

u/mikkele Aug 31 '08

I guess that what he meant was that either language will do nicely for most of the same tasks. Sure, Ruby has some features that Python does not have (and probably vice versa), but personal preferences aside, either language can be used for roughly the same purposes with roughly the same resulting quality and productivity.

So if you want to be a Ruby hacker, be a good Ruby hacker. Ditto for Python. Either way, it does not matter much these days.

1

u/thephotoman Aug 31 '08

That said, once you learn one, the other isn't too hard to learn. They both operate in essentially the same way, even if the syntax is different.

4

u/jmhodges Aug 31 '08

Oh, agreed, other than ruby's emphasis on blocks (plus the subtleties of lambdas vs. procs vs. blocks.. bleh)