Skip to content

Instantly share code, notes, and snippets.

@wycats
Created June 3, 2011 19:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wycats/1006999 to your computer and use it in GitHub Desktop.
Save wycats/1006999 to your computer and use it in GitHub Desktop.
# I don't necessarily love CoffeeScript as a template for ES.next, but here is an example
# of metaprogramming via a combination of:
# * "this" bound to the currently created class in class definitions
# * class methods inherited
class Foo
this.hasMany = (types) ->
this.prototype[types] = ->
alert "Looking up #{types}"
class Bar extends Foo
this.hasMany "friends"
new Bar().friends()
# Similar example in Ruby
class Foo
def self.has_many(types)
define_method(types) { puts "Looking up #{types}" }
end
end
class Bar < Foo
has_many "friends"
end
Bar.new.friends
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment