Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# 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
You can’t perform that action at this time.