Last active
December 11, 2015 13:58
-
-
Save rewinfrey/4611225 to your computer and use it in GitHub Desktop.
What Is Self?
This file contains 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 SelfMaker | |
def yet_what_is_self? | |
self.class.class_eval do | |
mirror(self, "Inside class_eval of SelfMaker#yet_what_is_self?") | |
end | |
instance_eval do | |
mirror(self, "Inside instance_eval of SelfMaker#yet_what_is_self?") | |
end | |
mirror(self, "Inside SelfMaker#yet_what_is_self?") | |
end | |
def mirror(for_self, context) | |
puts "Where Am I? #{context}" | |
puts "Who Am I? #{for_self} (My class is: #{for_self.class})" | |
puts "my object_id: #{for_self.object_id}\n" | |
end | |
def self.mirror(for_self, context) | |
self.new.mirror(for_self, context) | |
end | |
end | |
a_self_is_born = SelfMaker.new | |
a_self_is_born.yet_what_is_self? | |
# >> Where Am I? Inside class_eval of SelfMaker#yet_what_is_self? | |
# >> Who Am I? SelfMaker (My class is: Class) | |
# >> my object_id: 70352810302640 | |
# >> Where Am I? Inside instance_eval of SelfMaker#yet_what_is_self? | |
# >> Who Am I? #<SelfMaker:0x007ff89488b8c0> (My class is: SelfMaker) | |
# >> my object_id: 70352810302560 | |
# >> Where Am I? Inside SelfMaker#yet_what_is_self? | |
# >> Who Am I? #<SelfMaker:0x007ff89488b8c0> (My class is: SelfMaker) | |
# >> my object_id: 70352810302560 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Basically, doing
"foo".instance_eval{ self }
is what I meant.