Created
June 3, 2011 19:30
-
-
Save wycats/1006999 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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