Skip to content

Instantly share code, notes, and snippets.

@ysr23
Created June 14, 2012 11:27
Show Gist options
  • Save ysr23/2929747 to your computer and use it in GitHub Desktop.
Save ysr23/2929747 to your computer and use it in GitHub Desktop.
Grahams Homework Pt3 Self

Self is not so much a concept as an object. The default object.

There is always only one self, but what that is may change. Like a narrator in a book with multiple first-person identities.

puts "top level"
puts "self is #{self}"

class C
  puts "Class Definition Block:"
  puts "self is #{self}"

  def self.x
    puts "Class method C.x:"
    puts "self is #{self}"
  end 

  def m
    puts "Instance Method C#x:"
    puts "self is #{self}"
  end 
x
end 
c = C.new
c.m

this will return

top level
self is main
Class Definition Block:
self is C
Class method C.x:
self is C
Instance Method C#x:
self is #<C:0x0000010086c4e0>

So in the instance method call, the hex tells us self is an instance of C.

Within Singleton Methods self is the object to which that method belongs

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