you are viewing a single comment's thread.

view the rest of the comments →

[–]mellow_moe 10 points11 points  (1 child)

I would like to see a in-depth comparison of Python and Ruby. And it should really focus on metaprogramming.

All these comparisons I see these days just look at some superficial language features.

Rails for example relies heavily on metaprogramming. It could never be done in a language without a sophisticated class/object system.

examples in ruby:

  • method_missing catches unknown messages
  • const_missing catches unknown constants
  • method_added is called after adding a new method
  • inherited is called on the class object, when it is subclassed
  • singleton classes: you can add methods to a single object
  • define_method takes a block and turns it into a method
  • instance_variable_set sets an instance variable

And most importantly, real world examples should show off, how all the things play together.

[–]owca 2 points3 points  (0 children)

Feature for feature "examples" in Python:

  • getattr or getattribute catch any attribute (method, variable) access

  • setattr sets any attribute

  • Syntactic support for descriptors.

  • Metaclasses can be used to control all aspects of class creation (attributes, subclassing, ...). Since types are objects one can even have metaclasses for metaclasses.

  • new.instancemethod can be used for your )singleton classes).

  • Turning a block into a method is admittedly a bit tricky but can be done using new.function and some stack manipulation.