Skip to content

Instantly share code, notes, and snippets.

@DouglasAllen
Last active October 12, 2015 15:28
Show Gist options
  • Save DouglasAllen/4047902 to your computer and use it in GitHub Desktop.
Save DouglasAllen/4047902 to your computer and use it in GitHub Desktop.
An irb session with self so help yorselves rhyms with elves doesn't it?
class << self!
end
# => NoMethodError: undefined method `self!' for main:Object
class << self
end
# => nil
self.class
# => Object
Myclass = Class.new
# => Myclass
Myclass.class
# => Class
Myclass << self
end
# => NoMethodError: undefined method `<<' for Myclass:Class
class Myclass << self
end
# => SyntaxError: (irb):13: syntax error, unexpected tLSHFT, expecting '<' or ';' or '\n'
class Myclass < self
end
# => TypeError: superclass mismatch for class Myclass
Myclass = Object.new
# => warning: already initialized constant Myclass
# => #<Object:0x136f6b0>
Myobject = Object.new
# => #<Object:0x1022940>
Myobject.self
# => NoMethodError: undefined method `self' for #<Object:0x1022940>
Myobject.self!
# => NoMethodError: undefined method `self!' for #<Object:0x1022940>
Myobject < self
# => NoMethodError: undefined method `<' for #<Object:0x1022940>
Myobject << self
# => NoMethodError: undefined method `<<' for #<Object:0x1022940>
class Myobject < self
end
# => TypeError: Myobject is not a class
Myobject.class
# => Object
Myclass.class
# => Object
python
# => NameError: undefined local variable or method `python' for main:Object
eval "class<<self;p self!;end"
# => undefined method `self!' for #<Class:#<Object:0xb94dee3c>> Epiphany! tada! But didn't you just p yourself?
# This is what we want
eval "class<<self;p self;end
# => #<Class:#<Object:0xb94dee3c>>
self
# => main
self!
# => NoMethodError: undefined method `self!' for main:Object
Myclass = Class.new
# => Myclass
Myclass << self!
# => NoMethodError: undefined method `self!' for main:Object
self
# => main
class
# => SyntaxError: (irb):2: syntax error, unexpected $end
class ::self!
# => SyntaxError: unexpected tFID, expecting tIDENTIFIER or tCONSTANT
[16] pry(main)> class << self!
[16] pry(main)*
# baby wants something?
class.class
# => self
# K! nuff messin with banging my self
class << self
"a gallon of fresh milk".send :self
"Over?!"
end
class.self
# => SyntaxError: unexpected '.' did baby drop something?
"Goo goo says baby Ruby".send :self
# => "HAHA" Oh no it's too late!
@kotp
Copy link

kotp commented Nov 9, 2012

There we go... glad you saw it! Hope it helped.

@DouglasAllen
Copy link
Author

So what did _why the lucky stiff mean by class << self! in the baby talk image?

@DouglasAllen
Copy link
Author

Maybe?
def self!
"self bang is open then"
end

@DouglasAllen
Copy link
Author

[22] pry(main)> def self
[22] pry(main)* "self is self?"
[22] pry(main)* end
=> :self
[23] pry(main)> self
=> main
[24] pry(main)> self.send :self
=> "self is self?"

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