Python VS. Ruby for Metaprogramming

Python vs. Ruby for metaprogramming

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.

What does Ruby have that Python doesn't, and vice versa?

You can have code in the class definition in both Ruby and Python. However, in Ruby you have a reference to the class (self). In Python you don't have a reference to the class, as the class isn't defined yet.

An example:

class Kaka
puts self
end

self in this case is the class, and this code would print out "Kaka". There is no way to print out the class name or in other ways access the class from the class definition body in Python.

What is Ruby's analog to Python Metaclasses?

Ruby doesn't have metaclasses. There are some constructs in Ruby which some people sometimes wrongly call metaclasses but they aren't (which is a source of endless confusion).

However, there's a lot of ways to achieve the same results in Ruby that you would do with metaclasses. But without telling us what exactly you want to do, there's no telling what those mechanisms might be.

In short:

  • Ruby doesn't have metaclasses
  • Ruby doesn't have any one construct that corresponds to Python's metaclasses
  • Everything that Python can do with metaclasses can also be done in Ruby
  • But there is no single construct, you will use different constructs depending on what exactly you want to do
  • Any one of those constructs probably has other features as well that do not correspond to metaclasses (although they probably correspond to something else in Python)
  • While you can do anything in Ruby that you can do with metaclasses in Python, it might not necessarily be straightforward
  • Although often there will be a more Rubyish solution that is elegant
  • Last but not least: while you can do anything in Ruby that you can do with metaclasses in Python, doing it might not necessarily be The Ruby Way

So, what are metaclasses exactly? Well, they are classes of classes. So, let's take a step back: what are classes exactly?

Classes …

  • are factories for objects
  • define the behavior of objects
  • define on a metaphysical level what it means to be an instance of the class

For example, the Array class produces array objects, defines the behavior of arrays and defines what "array-ness" means.

Back to metaclasses.

Metaclasses …

  • are factories for classes
  • define the behavior of classes
  • define on a metaphysical level what it means to be a class

In Ruby, those three responsibilities are split across three different places:

  • the Class class creates classes and defines a little bit of the behavior
  • the individual class's eigenclass defines a little bit of the behavior of the class
  • the concept of "classness" is hardwired into the interpreter, which also implements the bulk of the behavior (for example, you cannot inherit from Class to create a new kind of class that looks up methods differently, or something like that – the method lookup algorithm is hardwired into the interpreter)

So, those three things together play the role of metaclasses, but neither one of those is a metaclass (each one only implements a small part of what a metaclass does), nor is the sum of those the metaclass (because they do much more than that).

Unfortunately, some people call eigenclasses of classes metaclasses. (Until recently, I was one of those misguided souls, until I finally saw the light.) Other people call all eigenclasses metaclasses. (Unfortunately, one of those people is the author of one the most popular tutorials on Ruby metaprogramming and the Ruby object model.) Some popular libraries add a metaclass method to Object that returns the object's eigenclass (e.g. ActiveSupport, Facets, metaid). Some people call all virtual classes (i.e. eigenclasses and include classes) metaclasses. Some people call Class the metaclass. Even within the Ruby source code itself, the word "metaclass" is used to refer to things that are not metaclasses.

Please advise on Ruby vs Python, for someone who likes LISP a lot

I'd go with Ruby. It's got all kinds of metaprogramming and duck punching hacks that make it really easy to extend. Features like blocks may not seem like much at first, but they make for some really clean syntax if you use them right. Open classes can be debugging hell if you screw them up, but if you're a responsible programmer, you can do things like 2.days.from_now (example from Rails) really easily (Python can do this too, I think, but with a bit more pain)

PS: Check out "Why Ruby is an acceptable LISP".

python and ruby - for what to use it?

They are good for mostly for rapid prototyping, quick development, dynamic programs, web applications and scripts. They're general purpose languages, so you can use them for pretty much everything you want. You'll have smaller development times (compared to, say, Java or C++), but worse performance and less static error-checking.

You can also develop desktop apps on them, but there may be some minor complications on shipping (since you'll usually have to ship the interpreter too).

You shouldn't do critical code or heavy computations on them - if you need these things, make them on a faster language (like C) and make a binding for the code. I believe Python is better for this than Ruby, but I could be wrong. (OTOH, Ruby has a stronger metaprogramming)

What languages support dynamic metaprogramming?

Most languages now days are moving towards providing that kind of functionality, but it's generally not as "clean" as it's in ruby.
All these languages have a lot of those capabilities (reference):

  • ActionScript
  • BASIC
  • BeanShell[3]
  • Clojure
  • ColdFusion
  • Common Lisp and most other Lisps
  • Groovy[4]
  • E programming language
  • JavaScript
  • VBScript
  • MATLAB / Octave
  • Lua
  • Objective-C
  • Perl
  • PHP
  • Powershell
  • Python
  • Ruby
  • Smalltalk
  • Tcl

Other languages such as Java and C# (reference) have ways of inspecting and creating code at run time, but it's not so "natural" as in those languages, and it feels a lot like a hack.



Related Topics



Leave a reply



Submit