Skip to content

Instantly share code, notes, and snippets.

@rewinfrey
Last active December 11, 2015 13:58
Show Gist options
  • Save rewinfrey/4611225 to your computer and use it in GitHub Desktop.
Save rewinfrey/4611225 to your computer and use it in GitHub Desktop.
What Is Self?
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
@manveru
Copy link

manveru commented Jan 23, 2013

Basically, doing "foo".instance_eval{ self } is what I meant.

@rewinfrey
Copy link
Author

Ah, I see :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment