Skip to content

Instantly share code, notes, and snippets.

@sumanmukherjee03
Created September 20, 2012 12:08
Show Gist options
  • Save sumanmukherjee03/3755516 to your computer and use it in GitHub Desktop.
Save sumanmukherjee03/3755516 to your computer and use it in GitHub Desktop.
Messing with bindings in ruby
# Coding hazard.
class CoOrdinate
attr_reader :x, :y
def initialize(x, y)
@x = x
@y = y
end
end
class InverseCoOrdinate < CoOrdinate
def initialize(x, y)
@x = y
@y = x
end
end
class JediCoOrdinate < InverseCoOrdinate
def initialize(x, y, invert = true)
invert ? super(x, y) : draw_light_saber(x, y)
end
def self.grandparent
self.superclass.superclass
end
private
def draw_light_saber(x, y)
super_init = self.class.grandparent.instance_method(:initialize).send(:bind, self)
super_init.call(x,y)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment