-
-
Save michaeldv/4274517 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
class Controller | |
attr_reader :tag | |
def initialize | |
@tag = "Controller" | |
@window = Window.new(self) | |
@window.show | |
hello | |
hello_again | |
end | |
end | |
class Window | |
attr_reader :tag | |
def initialize(controller) | |
@controller = controller | |
end | |
def show | |
@tag = "World" | |
@controller.instance_eval do | |
def hello | |
puts "Hello #{tag}!" # => Hello Controller! | |
end | |
end | |
# | |
# Perform outer self discovery within the instance method since | |
# evaluating strings is not supported in RubyMotion (RuntimeError) | |
# | |
@controller.instance_eval do | |
def hello_again | |
control = instance_variables.find do |var| | |
klass = instance_variable_get(var) | |
klass.is_a?(Window) && klass.instance_variable_get("@controller") == self | |
end | |
puts "Hello #{instance_variable_get(control).tag}!" # => Hello World! | |
end | |
end | |
# # Find controller's instance variable that is current self. | |
# window = @controller.instance_variables.find { |var| @controller.instance_variable_get(var) == self } | |
# | |
# # Now we can use the outer self within the instance_eval. | |
# @controller.instance_eval(%Q/ | |
# def hello_again | |
# tag = #{window}.tag | |
# puts "Hello #{tag}!" # => Hello World! | |
# end | |
# /) | |
end | |
end | |
Controller.new |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment