Err the Blog Atom Feed Icon
Err the Blog
Rubyisms and Railities
  • “irb Mix Tape”
    – Chris on September 08, 2006

    I used to make mix tapes (CDs) back in high school all the time. There’s nothing quite like spending hours upon hours picking 75 minutes worth of songs that fit together, flow nicely, and make you look cool to girls. Different types, too. Like a ‘slow song’ mix tape or a ‘punk rock’ mix tape or even a ‘moody-instrumentals-only’ mix tape. My favorite was the Quake mix, best enjoyed while playing Quake. Metal. You know Megadeth covered the Duke Nukem theme song, right? Fraggin’ bliss.

    Today I present a different type of mix tape: the irb Mix Tape. This one’s just for you. I wrote out the lyrics by hand and illustrated the insert. I hope it cheers you up on those long bus rides to school.

    irb mix tape

    Track 1: The C in MVC

    I’ll get to the good part right away for you Railers. The overture. We all know script/console is wonderful when playing with models, but what about those other letters in MVC? ActionPack needs love, too.

    Witness the love. First up, app. It’s a local variable you get for free in script/console. An instance of ActionController::Integration::Session, it’s a lot like those pseudo-controllers you bang around in functional tests. Again, witness:

    $ ruby script/console
    Loading development environment.
    >> app.url_for(:controller => 'stories', :action => 'show', :id => '10002')
    => "http://www.example.com/stories/10002"
    >> app.get '/stories/10002'
    => 200
    >> app.assigns(:story)
    => #<Story:0x24aad0c ... >
    >> app.path
    => "/stories/10002"
    >> app.reset!
    => nil
    >> app.get '/yeah/right/dude'
    => 404
    >> app.post '/account/login', { :username => 'defunkt', :password => 'razzletaz' }
    => 200
    >> app.cookies
    => {"_session_id"=>"9d1c014f42881524ff6cb81fb5b594bc"}
    

    Neat, huh?

    Track 2: The H in MVC

    Helpers? Yeah, we got that. Just like app, Rails gives you a helper local variable to play with. Like a little two minute bridge gearing you up for the rest of the tape.

    $ ruby script/console
    Loading development environment.
    >> helper.pluralize 2, "story"
    => "2 stories"
    >> helper.submit_tag
    => "<input name=\"commit\" type=\"submit\" value=\"Save changes\" />"
    >> helper.visual_effect :blindUp, 'post'
    => "new Effect.BlindUp(\"post\",{});"
    

    Etc.

    Track 3: Context

    Spawn irb subsessions in the context of an object. This track is loud and exciting, with a hint of lament. You can do it with normal irb which means you can also do it with script/console.

    $ ruby script/console
    Loading development environment.
    >> irb Story
    >> self
    => Story
    >> find(:first)
    => #<Story:0x2427c2c ... >
    >> quit
    >> irb Story.find(:first)
    >> title
    => "Mail-Order Desserts"
    >> quit
    => #<IRB::Irb: @scanner=#<RubyLex:0x249a36c>, @context=#<IRB::Context:0x249e534>, @signal_status=:IN_EVAL>
    >> irb app
    >> host
    => "www.example.com"
    

    It’s like you’re actually in the object. Any methods you call are getting called on the object whose context and privacy you have invaded. Saves some typing and gets you in with @instance_variables, too. Remember: you can use this in normal irb as well.

    Track 4: Sandboxin’

    No, not _why’s sandbox. The Rails script/console has a great feature which lets bungle your data without fear of ruining your precious information. Sandbox. The beat on this track is just killer.

    $ ruby script/console --sandbox
    Loading development environment in sandbox.
    Any modifications you make will be rolled back on exit.
    >> story = Story.find(:first)
    => #<story:0x244f0ec>
    >> story.title
    => "Mail-Order Desserts"
    >> story.title = "Snail Mail Droooolz!"
    => "Snail Mail Droooolz!"
    >> story.save
    => true
    >> Story.find(:first).title
    => "Snail Mail Droooolz!"
    >> quit
    $ ruby script/console
    Loading development environment.
    >> Story.find(:first).title
    => "Mail-Order Desserts"
    

    How wonderful! I love the timing on the snare.

    Track 5: Watson the Underbar

    There’s always this little guy floating around, very coyly, who can tell you the value of the last evaluated expression in irb. He’s like your Watson. I call him Underbar and he reminds me of a soothing acoustic melody.

    >> 1 + 1
    => 2
    >> _
    => 2
    >> _ * _
    => 4
    >> if _
    >> "#{_} is true!"
    >> else
    ?> "#{_} is false :("
    >> end
    => "4 is true!"
    

    What a chap.

    Track 7: Back Tracking

    Starting irb with --tracer will show you what Ruby is up to whenever you run a command. We quickly escape our relaxing acoustic instrumental and return to an up-tempo feat of guitar stringmanship.

    $ irb --tracer
    /usr/local/lib/ruby/1.8/tracer.rb:151: warning: tried to create Proc object without a block
    /usr/local/lib/ruby/1.8/tracer.rb:147: warning: tried to create Proc object without a block
    >> 1 + 1
    #0:(irb):1:Object:-: -
    #0:(irb):1:Fixnum:>: -
    #0:(irb):1:Fixnum:<: -> 2
    >> name = 'chris'
    #0:(irb):2:Object:-: -
    => "chris"
    

    Start irb with --tracer and enter something like require ‘rubygems’. I dare you.

    Track 8: bashish

    This one is metal, like the Duke.

    So you can jump into different contexts with irb, but you can also jump between different contexts. Like bash’s jobs and fg, irb comes fully loaded with its own jobs and fg.

    Use jobs to see all the open subsessions. Each one has a number. Use fg # to jump to that subsession while keeping your current one running somewhere in background-land. To halt any of the subsessions, use kill #. Here’s something like the script/console session I had going earlier:

    $ ruby script/console
    Loading development environment.
    >> irb Story
    >> self
    => Story
    >> jobs
    => #0->irb on main (#<thread:0x1db740>: stop)
    #1->irb#1 on Story (#<thread:0x2439558>: running)
    >> fg 0
    => #<irb::irb:><rubylex:0x249a36c>, @context=#<irb::context:0x249e534>, @signal_status=:IN_EVAL>
    >> irb helper
    >> jobs
    => #0->irb on main (#<thread:0x1db740>: stop)
    #1->irb#1 on Story (#<thread:0x2439558>: stop)
    #2->irb#2 on #<object:0x249c3c4> (#<thread:0x2410bbc>: running)
    >> fg 1
    => #<irb::irb:><rubylex:0x24393c8>, @context=#<irb::context:0x24393dc>, @signal_status=:IN_EVAL>
    >> self
    => Story
    >> fg 2
    => #<irb::irb:><rubylex:0x2410b08>, @context=#<irb::context:0x2410b1c>, @signal_status=:IN_EVAL>
    >> self
    => #<object:0x249c3c4>
    >> kill 1
    => [1]
    >> jobs
    => #0->irb on main (#<thread:0x1db740>: running)
    #2->irb#2 on #<object:0x249c3c4> (#<thread:0x2424f68>: running)
    

    Honestly, kill doesn’t seem to work right for me. But no matter. If you already have any of these methods defined on your object you can just use irb_jobs, irb_fg, or irb_kill. Thanks, Ruby.

    Epilogue

    The metal lead fades out and we embark on an epic climax. See, there’s a lot of really great irb stuff out there. _why in particular seems very interested in the holiest of holies. I trust you’ve seen most of his RedHanded posts, but if not you can start with MethodFinder or Tab Completion in irb. He’s got a chapter on irb in his book as do, naturally, the Praggies.

    Bloggers. Ruby-o-sphere. First you’ve got a cross-session command history from Nick Sieger, a staple. For the adventurous Graeme Mathieson is crazy enough to chase the shell in irb and Paul Battley has a neat iterative development in irb trick. While we’re at it, why don’t you grab yourself a simple irb prompt on your way out?

    We’re just touching the surface here, really. You have any song suggestions? Did I miss a killer track? Surely. Either way, enjoy this song selection. Maybe I’ll make you another mix tape someday. Someday.

  • Dr Nic, about 5 hours later:

    So… much… magic!

  • FastJack, about 4 hours later:

    Wait! What happened to Track 6?

  • atmos, 17 minutes later:

    Gotta love mixtapes. Nice writeup. :)

  • Nick, 33 minutes later:

    And what’s on the hidden track? Let me know when you find out.

  • Chris, about 1 hour later:

    What happened to Track 6????

  • Chris, about 6 hours later:

    Okay here’s one of many hidden tracks. This one sounds a lot like the Outkast song where he keeps saying, “forever, forever-ever, forever-ever.”

    >> 1 / (0 / 1.0)
    => Infinity
    >> 100**100**100
    => Infinity
    
  • Adam Sanderson, 1 day later:

    Hey those rails tricks are pretty neat. I somehow missed that.

    Thanks. Time to go play with irb now.

  • Dieter Komendera, 2 days later:

    Thanks for this great article! Very nice.

  • Vinicius Teles, 2 days later:

    Thanks for this great post. Very helpful.

  • Seth Thomas Rasmussen, 2 days later:

    Fucking nice work, man. You’ve gone and turned my desire to curse this evening for the better. And thanks for the love, especially.

  • Markus, 3 days later:

    How can I access the helpers that I defined myself?

  • colabus, 3 days later:

    Excellent man, always wanted to know how script/console accessed the application. Cheers :)

  • Chris, 9 days later:

    Appreciate all your helpful information and I would like to print it out.

    However, how would it be possible that when one prints out the code in your post that instead of being TRUNCATED, the code WRAPS?

  • ohyeah, 11 days later:

    Podcasting is the new mixtape.

  • mathie, 23 days later:

    Oh, wow, I didn’t know that. I’m gonna be humming track 2 all day!

  • bitbutter, 2 months later:

    great article!

    How can I access the helpers that I defined myself?

    is this possible somehow? it’d be very useful. cheers.

  • Abon, 4 months later:

    I’m trying to test if I can test the GET with parameters I want to fill in the form. Anyone knows the formatting?

  • Daniel, 5 months later:

    nice long article really usefull

  • chosi, about 1 year later:

    sweet!

  • AJ, about 1 year later:

    Awesome Post!

  • Twenty people have commented.
    Chime in.
    Sorry, no more comments :(
This is Err, the weblog of PJ Hyett and Chris Wanstrath.
All original content copyright ©2006-2008 the aforementioned.