
Ruby lib/irb/easter-egg.rb - syspec
https://github.com/ruby/ruby/blob/a0c7c23c9cec0d0ffcba012279cd652d28ad5bf3/lib/irb/easter-egg.rb
======
derefr
A tangent, but: I've read and written a lot of Ruby—except never much of the
stdlib, for some reason—and I've literally never seen anyone use the `private`
pseudo-keyword in the Java-esque way it's used here (`private def foo...` on
the same line), to the point that I didn't even realize that that was
possible.

I've always only seen these usages:

• `private` on its own line with surrounding whitespace, treated as a section-
break in the class, where it has a "mode-switch for all that follows" effect
similar to `private:` in C++.

• `private :foo` on a separate line _after_ `def foo...`, where it's just
acting as a standalone call to one of Class's "builder" DSL methods, like an
`attr_reader` call.

• `private\ndef foo` (rare), as if `private` were some Java-like annotation
modifying just the following method (though this is really just the first
mode-switching kind, and later methods will need a `public\n` "annotation" to
go back.)

Why is that? It seems startlingly obvious, once you see it done, that `private
def ...` is the idiomatic way to use it. (After all, they bothered to make
`def` return the symbolic name of the function you defined, just so you could
chain it into class-builder-methods like this.)

~~~
dragonwriter
> It seems startlingly obvious, once you see it done, that `private def ...`
> is the idiomatic way to use it.

As you note that almost no one does it in Ruby, it is _not_ the idiomatic way
in Ruby. Idioms change slowly, and this wasn't supported for most of Ruby
history.

------
sdwolfz
Here's a one liner:

    
    
        ruby -r irb -e 'IRB.send(:easter_egg)'
    

And in Docker you get a static image, but sometimes, randomly you get the
dynamic one...

    
    
        docker run --rm -it ruby:2.7.1-alpine ruby -r irb -e 'IRB.send(:easter_egg)'

~~~
jsjohnst
Make it:

IRB.send(:easter_egg, :dancing)

And you should always get the animated one.

~~~
sdwolfz
Yes, can confirm it works without failure!

------
syspec
I stumbled onto this when looking at the ruby source code, anyone know how to
trigger it?

~~~
q3k
On Ruby 2.7+, run irb, type 'RubyVM', press tab twice.

~~~
syspec
Thanks! Very cool - here's a gif for the curious:
[https://media1.giphy.com/media/L0eN2GwpS7GOmv5Kk7/giphy.gif](https://media1.giphy.com/media/L0eN2GwpS7GOmv5Kk7/giphy.gif)

