Python vs. Ruby for metaprogramming

34,248

Solution 1

There's not really a huge difference between python and ruby at least at an ideological level. For the most part, they're just different flavors of the same thing. Thus, I would recommend seeing which one matches your programming style more.

Solution 2

I've read up on Lisp a little and I would love to find a language that allows some of the cool stuff that Lisp does, but without the strange syntax, etc. of Lisp.

Wouldn't we all.

minimal distinction between code and data, Lisp style

Sadly, the minimal distinction between code and data and "strange" syntax are consequences of each other.

If you want easy-to-read syntax, you have Python. However, the code is not represented in any of the commonly-used built-in data structures. It fails—as most languages do—in item #1 of your 'important' list. That makes it difficult to provide useful help.

You can't have it all. Remember, you aren't the first to have this thought. If something like your ideal language existed, we'd all be using it. Since the real world falls short of your ideals, you'll have to re-prioritize your wish list. The "important" section has to be rearranged to identify what's really important to you.

Solution 3

Honestly, as far as metaprogramming facilities go, Ruby and Python are a lot more similar than some of their adherent like to admit. This review of both language offers a pretty good comparison/review:

So, just pick one based on some criteria. Maybe you like Rails and want to study that code. Maybe SciPy is your thing. Look at the ecosystem of libraries, community, etc, and pick one. You certainly won't lose out on some metaprogramming nirvana based on your choice of either.

Solution 4

Disclaimer: I only dabble in either language, but I have at least written small working programs (not just quick scripts, for which I use Perl, bash or GNU make) in both.

Ruby can be really nice for the "multiple paradigms" point 3, because it works hard to make it easy to create domain-specific languages. For example, browse online and look at a couple of bits of Ruby on Rails code, and a couple of bits of Rake code. They're both Ruby, and you can see the similarities, but they don't look like what you'd normally think of as the same language.

Python seems to me to be a bit more predictable (possibly correlated to 'clean' and 'sane' point 2), but I don't really know whether that's because of the language itself or just that it's typically used by people with different values. I have never attempted deep magic in Python. I would certainly say that both languages are well thought out.

Both score well in 1 and 4. [Edit: actually 1 is pretty arguable - there is "eval" in both, as common in interpreted languages, but they're hardly conceptually pure. You can define closures, assign methods to objects, and whatnot. Not sure whether this goes as far as you want.]

Personally I find Ruby more fun, but in part that's because it's easier to get distracted thinking of cool ways to do things. I've actually used Python more. Sometimes you don't want cool, you want to get on with it so it's done before bedtime...

Neither of them is difficult to get into, so you could just decide to do your next minor task in one, and the one after that in the other. Or pick up an introductory book on each from the library, skim-read them both and see what grabs you.

Solution 5

Have you considered Smalltalk? It offers a very simple, clear and extensible syntax with reflectivity and introspection capabilities and a fully integrated development environment that takes advantage of those capabilities. Have a look at some of the work being done in Squeak Smalltalk for instance. A lot of researchers using Squeak hang out on the Squeak mailing list and #squeak on freenode, so you can get help on complex issues very easily.

Other indicators of its current relevance: it runs on any platform you'd care to name (including the iPhone); Gilad Bracha is basing his Newspeak work on Squeak; the V8 team cut their teeth on Smalltalk VMs; and Dan Ingalls and Randal Schwartz have recently returned to Smalltalk work after years in the wilderness.

Best of luck with your search - let us know what you decide in the end.

Share:
34,248
dsimcha
Author by

dsimcha

Updated on July 27, 2020

Comments

  • dsimcha
    dsimcha almost 4 years

    I'm currently primarily a D programmer and am looking to add another language to my toolbox, preferably one that supports the metaprogramming hacks that just can't be done in a statically compiled language like D.

    I've read up on Lisp a little and I would love to find a language that allows some of the cool stuff that Lisp does, but without the strange syntax, etc. of Lisp. I don't want to start a language flame war, and I'm sure both Ruby and Python have their tradeoffs, so I'll list what's important to me personally. Please tell me whether Ruby, Python, or some other language would be best for me.

    Important:

    1. Good metaprogramming. Ability to create classes, methods, functions, etc. at runtime. Preferably, minimal distinction between code and data, Lisp style.
    2. Nice, clean, sane syntax and consistent, intuitive semantics. Basically a well thought-out, fun to use, modern language.
    3. Multiple paradigms. No one paradigm is right for every project, or even every small subproblem within a project.
    4. An interesting language that actually affects the way one thinks about programming.

    Somewhat important:

    1. Performance. It would be nice if performance was decent, but when performance is a real priority, I'll use D instead.
    2. Well-documented.

    Not important:

    1. Community size, library availability, etc. None of these are characteristics of the language itself, and all can change very quickly.
    2. Job availability. I am not a full-time, professional programmer. I am a grad student and programming is tangentially relevant to my research.
    3. Any features that are primarily designed with very large projects worked on by a million code monkeys in mind.
  • pupeno
    pupeno over 15 years
    I've found that many languages implement Lisp-like macros in non-Lispy syntaxes and what ends happening is that writing macros there is very hard, because nobody naturally knows the data structures in which the code is represented, thus writing macros gets too hard and nobody does it.
  • pupeno
    pupeno over 15 years
    Or you can try Clojure, which I find very nice.
  • Jules
    Jules over 15 years
    People find Lisp hard to read because they're not familiar with the syntax. I find Lisp much easier to read than C# (but harder than Python).
  • jfs
    jfs over 15 years
    Python is not whitespace-sensitive It is indentation-sensitive.
  • orion
    orion almost 15 years
    OCaml allows you to create classes/methods at runtime? How does that work?
  • flq
    flq almost 15 years
    Thanks for that, didn't know it. Looks fun at first glance, maybe plenty of (()) but anyway. Hope I'll find the time to have a look, soon.
  • Robert Vuković
    Robert Vuković almost 15 years
    I just read about OCaml and maybe it can not create stuff at runtime so I have removed it.
  • gorsky
    gorsky over 14 years
    -1 I'm using Python and it fits perfectly, differences between Python and Ruby lays in other aspects. Fanboy-ish noisemaker, you are.
  • FelipeC
    FelipeC over 14 years
    A lot of talk but no walk. Care to provide an example in Python of adding a method dynamically to say, the String class?
  • Admin
    Admin over 14 years
    I would agree with gorsky that this is a fan-boyish response. I'm not claiming to be an expert, but after reading all of the (more realistic) comments on this page, all seem to say the same thing. That no language is perfect and that there will not be one single language that will meet all of the needs listed above. Your response seems to make that claim. Although ruby may in fact be a good choice, I would not champion it as the de-facto language. And to say that Pythons doesn't fit the important points seems a little rash.
  • FelipeC
    FelipeC over 14 years
    Ruby is definitely not perfect, and definitely not the right choice on many situations. but the question listed 4 points, and Ruby meets them perfectly, Python does not. I somebody is interested I can provide examples of how Python doesn't meet any of those points. Other languages might, but not Python.
  • FelipeC
    FelipeC over 14 years
    There're most definitely not the same thing. The look similar on the surface, but one you exercise the most powerful features of Ruby you understand that Python is just no match. For an example try to write a DSL in Ruby vs writing one Python, or creating function, methods, classes, etc. at run-time. It's much more straight-forward in Ruby.
  • Admin
    Admin over 14 years
    oh, and I found an example of the quesiton you posted above (^) : code.activestate.com/recipes/81732
  • Lennart Regebro
    Lennart Regebro over 14 years
    @felipec The standard str class can't have methods added, basically for efficiency reasons, and secondly because you don't need it. If you need a string class that you can add methods to, you can simply subclass it. That would give you a class where you can dynamically add methods, should you need it.
  • Stephen Cagle
    Stephen Cagle over 14 years
    Yes, but Jason's point still stands. Most people are using, Ruby, Python, Perl, whatever to do "normal" programming task. It is rare that you absolutely need to use dynamic programming, it is even rarer that you require your own custom defined DSL. If you stay out of the dynamic world, by and large, ruby and Python, and even Perl to a lesser degree, are pretty similar.
  • Wayne Conrad
    Wayne Conrad over 14 years
    It's not rare that you need to do metaprogramming, it's just rare that it's done. All but the most trivial program have repeating patterns that don't fall to the usual refactoring tools but could be slain readily by metaprogramming.
  • FelipeC
    FelipeC over 14 years
    @lennart How do you know I don't need it? Have you carefully considered my specific meta-programing needs? In ruby it's easy to do something "640x480".to_framesize. It might be useful in order to pass a bunch of object to a method that doesn't care about the class, but just does obj.to_framesize. Sure, there are alternative ways to do it, but more complicated and uglier.
  • FelipeC
    FelipeC over 14 years
    @john That's precisely my point; it's very complicated and ugly (not to mention impossible for the String class). OTOH in Ruby it's very simple: "self.class.send(:define_method, :method_name) { method_code }"
  • Ken
    Ken almost 14 years
    Ruby may try to follow the PoLS, but I wouldn't say it does. For example, the lambda/Proc.new mess has been called "surprising behavior" and "highly counterintuitive" here on SO. :-) Any language as big and complex as Ruby is bound to have such confusing areas.
  • Sean Copenhaver
    Sean Copenhaver almost 14 years
    Ruby and Python are hugely different even at ideas that govern their design. Python they want one and hopefully one obvious way to do things. That generally makes the language not as expressive as Ruby, but it makes it more consistent. Ruby comes a little more from the Perl way of things where there are many ways to do things. Also Ruby makes some things super easy and actually includes the idea of private members. Python on the other hand at the most just makes some thing harder to do, so you have to be more explicit (like adding or overriding behavior on classes).
  • Sean Copenhaver
    Sean Copenhaver almost 14 years
    You should probably do a crash course in both, but for easy metaprogramming it appears that Ruby is more suited. I don't have very much experience though, so take that with a grain of salt.
  • FelipeC
    FelipeC almost 14 years
    Well, sure, no language can match the predispositions of everybody, whoever, the best it can do is try, and Ruby does... Python on the other hand doesn't. " ".join? really?
  • SingleNegationElimination
    SingleNegationElimination over 13 years
    I'm having a very hard time with this new method on builtin string class strawman. Can someone provide an example where the builtin string types (plural, python has more than one string type) must be monkey patched; and subclassing them wouldn't do?
  • Rainer Joswig
    Rainer Joswig over 13 years
    (format nil "~{~a~^~%~}" (remove nil (mapcar #'description mylist)))
  • Ken
    Ken over 13 years
    felipec: Yes, really. " ".join(...) seemed weird for about 2 minutes, until I realized that I like being able to define my own enumerators, but I don't want to have to define my own join for each one. lambda/Proc.new in Ruby seems pointless and confusing even after using the language for several years. I don't see how you can claim that one "tries" and the other "doesn't" -- everybody tries.
  • FelipeC
    FelipeC over 13 years
    Ken: The fact that you found a "reason" doesn't mean it's any less weird. However, it's barely a valid reason: if join was part of the base enumerator class, then you would inherit it automatically.
  • FelipeC
    FelipeC over 13 years
    And no, python doesn't follow the POLA. Another example is "if elem = hash['key']", you would expect elem to be null, and the if block not executed, not an exception. Also, I don't know what you mean by lambda/Proc.new, it's often said that Python lamba's are not as good as Ruby's: stackoverflow.com/questions/2654425/…
  • Andrew Burns
    Andrew Burns over 13 years
    @Stephen dsimcha specifically stated that he was interested in metaprogramming so it is very relevant to this conversation.
  • P Shved
    P Shved about 13 years
    @Token here's an example of monkey-patching String in Ruby for metaprogramming purposes: coldattic.info/shvedsky/pro/blogs/a-foo-walks-into-a-bar/pos‌​ts/…. Subclassing wouldn't do; however, a simple two-argument function would.
  • Dan Burton
    Dan Burton about 13 years
    Strong agree. If you want the power of Lisp, just dive in and have it! It's actually quite easy to get used to the parens; they're not as big a deal as most people make them out to be.
  • Gabi Purcaru
    Gabi Purcaru almost 13 years
    Ruby and Python are only similar in the fact that they preach "beautiful code". They just have totally different views on that beauty thing (which IMO is good)
  • inger
    inger almost 13 years
    "Yes, but Jason's point still stands." Maybe in general but not as an answer to this, and this seems an ignorant one. The question was not about "ideological" levels, it was about metaprogramming. This is one area where the usual Ruby/Python/whatever generalisation doesn't hold. "it is even rarer that you require your own custom defined DSL": this depends a lot on what you're in.And maybe you don't design DSLs everyday,but,even Rails developers use them all the time. Let alone Lisp, without fancy 3 letter acronyms you just raise the language's abstraction level to your problem domain.
  • inger
    inger almost 13 years
    nice, but where is metaprogramming here? it seems to be some slightly functional style, remotely related to the question.
  • inger
    inger almost 13 years
    I largely agree with the point, but my experience of both Ruby and Lisp shows that former is as good for metaprogramming as it gets without the parentheses. Other alternatives that come close could be TCL and JavaScript but I would not favor them for other reasons.
  • inger
    inger almost 13 years
    that blog entry seems to have more to do with personal preferences (fair enough but beauty is in the eye of the beholder), rather than metaprogramming - which was the main point of the OP.
  • Friedrich
    Friedrich over 6 years
    Long long agi there was a language called Dylan which should be exactly that.