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
Tweet

More Decks by Mike Mondragon

Other Decks in Programming

Transcript

  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. plain old irb Tuesday, January 8, 13 First, plain old

    irb, e.g. - no customization with command line arguments or irbrc
  12. >> 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.
  13. irb + interactive editor (Vim) + extras Tuesday, January 8,

    13 I prefer a simple irbrc with a number of extra configurations.
  14. •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.
  15. >> 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.
  16. irb + irbtools gem Tuesday, January 8, 13 @janlelis has

    taken the time to collate the best mods for irb into the irbtools gem
  17. • 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
  18. 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
  19. 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