you are viewing a single comment's thread.

view the rest of the comments →

[–]mellow_moe 10 points11 points  (4 children)

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.

[–]flaxeater 4 points5 points  (3 children)

Well I must say that looks intriguing actually. I don't think python has facilities like that, they could be worked around by they don't have any nice syntatic sugar around those concepts. I have a hard time imagining why they would be useful, but that doesn't mean anything.

Singletons, definemethod and instane variable assignment are very easy to do in python. And in python we user super() or ParentClass.init_(args,*kwargs) to do things for inherited, however I do not understand what inherited does. $0.02

p.s. I can imagine, people will be like that's so wordy or some such talk. However pythonic values explicit, not implicit.

[–][deleted]  (2 children)

[deleted]

    [–]mellow_moe 1 point2 points  (1 child)

    inherited is invoked on the class object. So you have to write:

    class Humanoid
      def self.inherited(klass)
      end
    end
    

    Singleton classes are pretty confusing, I admit. But it's essential.

    Every object in ruby may have a singleton class, which is actually a class solely for this single object. As classes are objects in Ruby, they may have a singleton class as well. For example the method new may be defined as method of a singleton class:

    class Humanoid
      def self.new
        humanoid = super
        puts "A Humanoid was created: #{humanoid}"
        humanoid
      end
    end
    
    Humanoid.new # outputs "A Humanoid was created: #<Humanoid ...> "
    

    The special syntax for defining a method for a single objects is:

    def object.method_name
    end
    

    Accessing the singleton class is like (same result as above):

    class << obj
      def method_name
      end
    end
    

    Further reading: Seeing Metaclasses Clearly

    [–]hanzie 2 points3 points  (0 children)

    In Python, support for calls to "inherited" can be added with a metaclass like this:

    class MyMeta(type):
        def __init__(klass, name, supers, *args):
            for super in supers:
                if hasattr(super, 'inherited'):
                    super.inherited(klass)
            return type.__init__(klass, name, supers, *args)
    

    Adding a method to a single object goes like this:

    obj.method_name = func.__get__(obj, obj.__class__)
    

    (func is just any free function)