News for hackers. Stuff that matters.
Re:Ask HN: What does Ruby have that Python doesn't? by mjw on Saturday August 23, 2008 @08:28AM (#284591)
OK. Like so:

module SpecialBehavioursForClass def extended(klass) klass.class_eval { ...do anything you like to classes including these behaviours as though you're in their class definition block itself... } end

  def special_class_method
    ...
  end

  def give_this_class_an_attribute(x)
    attr_accessor :x
  end
end

then

Class MySpecialClass extend SpecialBehavioursForClass

  give_this_class_an_attribute :abc
end

(note how there's already a class method 'attr_accessor' which does this - but just to reinforce how you'd go about making something similar yourself)

then: MySpecialClass.is_a?(SpecialBehavioursForClass) returns true.

However, MySpecialClass.class == Class it's not actually an instance of a metaclass as these don't exist in Ruby.

You can achieve the exact same things though, using Modules, class methods, and the .inherited / .included / .extended callbacks.

Re:Ask HN: What does Ruby have that Python doesn't? by mjw on Saturday August 23, 2008 @08:29AM (#284594)
Sorry it bolloxed my formatting. Note to these people: tell us how your code formatting works, somewhere near the goddamn input field