Skip to content

Instantly share code, notes, and snippets.

@NV
Created January 30, 2011 19:18
Show Gist options
  • Save NV/803138 to your computer and use it in GitHub Desktop.
Save NV/803138 to your computer and use it in GitHub Desktop.
Object-Specific Classes in Ruby and JavaScript (via non-standard __proto__)
rectangle = {
width: 10,
height: 8
}
rectangle.__proto__ = {
area: function() {
return this.width * this.height
}
}
rectangle.area() // 80
Object.keys(rectangle) // ['width', 'height']
rectangle = {
:width => 10,
:height => 8
}
class << rectangle
def area
self[:width] * self[:height]
end
end
rectangle.area # 80
rectangle.keys # [:width, :height]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment