Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Seattle.rb - IRB Presentation

Seattle.rb - IRB Presentation

My slide for a lightening talk on IRB (Interactive Ruby Shell) for Seattle.rb 01/08/2013

Mike Mondragon

January 08, 2013

More Decks by Mike Mondragon

Other Decks in Programming


  1. irb tricks Mike Mondragon @monde Tuesday, January 8, 13 I’m

    Mike Mondragon, @monde on the twitters, github, etc.
  2. irb Interactive Ruby Shell Tuesday, January 8, 13 Today I’ll

    be speaking about IRB - the Interactive Ruby Shell
  3. bit.ly/seattle-rb-irb additional information and code Tuesday, January 8, 13 A

    gist with my notes and example IRB RC files can be found at this shortened URL http:// bit.ly/seattle-rb-irb
  4. irb is a REPL (read-eval-print loop) Tuesday, January 8, 13

    IRB is a REPL - a Read-Eval-Print Loop
  5. Tuesday, January 8, 13 Ruby code can by typed directly

    into the console or paste it in. The new line character signals each line of code to be evaluated so this can be invoked with a key press or pasting code. The results of each line are evaluated are printed back to the console. The console maintains a context so definitions, objects, and variables continue to exist in each console session.
  6. typed input >> p "I typed this" "I typed this"

    => "I typed this" >> puts "I typed this" I typed this => nil Tuesday, January 8, 13 This is a typed input example. Notice that each line is evaluated after the shell reads the new line character. Also, take note how the result of each evaluation is returned to the shell. The ruby function p prints a string and returns the value of it’s input inspected. The ruby function puts prints a string and returns nothing.
  7. pasted input >> puts "I pasted this" I pasted this

    => nil Tuesday, January 8, 13 This is a pasted input example in which the input string has new lines with in it. The console prints back each line but does not evaluate the code until a valid statement has been reached.
  8. retained context >> a = 1 => 1 >> b

    = a+2 => 3 >> c = b+3 => 6 >> b => 3 Tuesday, January 8, 13 This example illustrates how variables are retained in the shell’s context. variables b and c and based on previous variables for example.
  9. retained context >> def rp(s); p s.reverse; end => nil

    >> rp "hello" "olleh" => "olleh" Tuesday, January 8, 13 The context will retain everything, even methods defined within it.
  10. •Learning •Practicing •Experiment ation •Testing •Application console Utility Tuesday, January

    8, 13 IRB is a very useful utility. You can use it for learning, experimentation, and even as a rudimentary application console.
  11. customization ~/.irbrc Tuesday, January 8, 13 Customize your irb to

    get the best utility from it.
  12. plain old irb Tuesday, January 8, 13 First, plain old

    irb, e.g. - no customization with command line arguments or irbrc
  13. >> puts "plain old irb" plain old irb => nil

    Tuesday, January 8, 13 Without an ~/.irbrc is not much more than a REPLE with a prompt. It doesn’t load anything or save it’s history at exit.
  14. irb + interactive editor (Vim) + extras Tuesday, January 8,

    13 I prefer a simple irbrc with a number of extra configurations.
  15. •vim edit/paste code •load/save command history •tab completion on object

    methods Tuesday, January 8, 13 I like to have my command history loaded and saved, the ability to edit code with vim, and tab completion on objects.
  16. >> h = {:a=>7, :zz=>"top"} => {:a=>7, :zz=>"top"} >> h.vim

    --- :a: 89 :zz: top => {:a=>89, :zz=>"top"} >> j = _ => {:a=>89, :zz=>"top"} Tuesday, January 8, 13 An example of irb with interactive vim. Notice editing the object opens vim on the object serialized to yaml and that it returns a new object. Also notice the underscore variable is the last returned value and I assign that value to the variable j.
  17. irb + irbtools gem Tuesday, January 8, 13 @janlelis has

    taken the time to collate the best mods for irb into the irbtools gem
  18. Tuesday, January 8, 13 Here’s an example of colorized output

    from the included wirb gem.
  19. • code and output colorized • method locator • vim

    edit/paste code • load/save command history • tab completion on object methods • lots of other extras ... Tuesday, January 8, 13 irbtools has many features, code and output colorization, method locator, interactive editor, and more. I think it’s best to start with your own small irbrc and modify to suit your needs. Once you have a better grasp of the potential modifications
  20. Thank You! Twitter/Github @monde Blog http://plasti.cx/ Tuesday, January 8, 13

    Thank you.
  21. bit.ly/seattle-rb-irb additional information and code Tuesday, January 8, 13 A

    gist with my notes and example IRB RC files can be found at this shortened URL http:// bit.ly/seattle-rb-irb
  22. Skinny Wallace Fat Wallace Tuesday, January 8, 13

  23. Image Credits • REPL + ICP + wycats - taken

    by me at a Ember.js meetup • Nerd on a bike - http://cycling-review.com/2010/04/bike- snob/ • Nerd on a hover bike - http://iamasupernerd.com/science- nerd/real-hover-bike/ • Ork Battlewagon - http://whitescars-army.blogspot.com/ 2011/07/defeating-ork-battlewagon-list-part-1.html • Modern Jeb Clampett - http://www.cybersalt.org/funny-car- pictures/jed-clampett-2 Tuesday, January 8, 13