Skip to content

Instantly share code, notes, and snippets.

@DouglasAllen
Last active October 12, 2015 15:28
Show Gist options
  • Select an option

  • Save DouglasAllen/4047902 to your computer and use it in GitHub Desktop.

Select an option

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!
@andredublin

Copy link
Copy Markdown

self! is not a valid method because self is a keyword. the bang operator denotes that you are applying a dangerous method ( one that modifies it's receiver ).

@kotp

kotp commented Nov 9, 2012

Copy link
Copy Markdown
>> class << self!
>> class
>> class.class
>> self

In these last three, where you are getting the >> prompt, it hasn't finished defning the class that you started to define. Finish defining it by adding and end and possibly another end (there were two class definitions you started here. That should bring up any errors (and you should have an error here).

@kotp

kotp commented Nov 9, 2012

Copy link
Copy Markdown

I assumed that he read the error messages from the second one and understood that part of the story. To carry your explanation further, and more to the point, the lowercase letter designates that it is either a local variable or a method in the first place. We never get to the point to worry about the bang part of the method name.

@kotp

kotp commented Nov 9, 2012

Copy link
Copy Markdown

About self, it is true what you say... however, it isn't the whole story....

>> def self    
>> puts "HAHA" 
>> end         
=> nil         
>> self        
=> main        
>> self.send(:s
HAHA           
=> nil         

@kotp

kotp commented Nov 9, 2012

Copy link
Copy Markdown

Whoops... that 3rd line from the end is supposed to be:

>> self.send(:self)

@DouglasAllen

Copy link
Copy Markdown
Author

I never seem to get the whole story till some time later and it comes like a revelation. HAHA!

@kotp

kotp commented Nov 9, 2012

Copy link
Copy Markdown

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

@DouglasAllen

Copy link
Copy Markdown
Author

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

@DouglasAllen

Copy link
Copy Markdown
Author

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

@DouglasAllen

Copy link
Copy Markdown
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