Create a gist now

Instantly share code, notes, and snippets.

notes for IRB lightening talk at Seattle.rb

IRB (Interactive Ruby Shell) notes

Notes for my irb lightening talk at Seattle.rb

Programming Ruby / The Pragmatic Programmer's Guide / Interactive Ruby Shell

interactive editor gem

interactive editor in irb (vim, etc.)

The Tiger's Vest (with a Basic Introduction to Irb)

irbtools gem

irbtools is a meta gem that installs a bunch of useful irb gems and configures them for you.

REPL

Read–eval–print loop

http://en.wikipedia.org/wiki/REPL

% irb
irb(main):001:0> IRB.conf
=> CONF[:AP_NAME]="irb"
CONF[:AT_EXIT]=[]
CONF[:AUTO_INDENT]=false
CONF[:BACK_TRACE_LIMIT]=16
CONF[:CONTEXT_MODE]=3
CONF[:DEBUG_LEVEL]=1
CONF[:ECHO]=nil
CONF[:EVAL_HISTORY]=nil
CONF[:IGNORE_EOF]=false
CONF[:IGNORE_SIGINT]=true
CONF[:INSPECT_MODE]=true
CONF[:IRB_LIB_PATH]="/Users/mikemondragon/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/irb"
CONF[:IRB_NAME]="irb"
CONF[:IRB_RC]=nil
CONF[:LC_MESSAGES]=#<IRB::Locale:0x007fd1e303b710 @modifier=nil, @encoding_name="UTF-8", @territory="US", @lang="en", @locale="en_US.UTF-8", @encoding=#<Encoding:UTF-8>>
CONF[:LOAD_MODULES]=[]
CONF[:MAIN_CONTEXT]=...myself...
CONF[:MATH_MODE]=false
CONF[:PROMPT]={:NULL=>{:PROMPT_I=>nil, :PROMPT_N=>nil, :PROMPT_S=>nil, :PROMPT_C=>nil, :RETURN=>"%s\n"}, :DEFAULT=>{:PROMPT_I=>"%N(%m):%03n:%i> ", :PROMPT_N=>"%N(%m):%03n:%i> ", :PROMPT_S=>"%N(%m):%03n:%i%l ", :PROMPT_C=>"%N(%m):%03n:%i* ", :RETURN=>"=> %s\n"}, :CLASSIC=>{:PROMPT_I=>"%N(%m):%03n:%i> ", :PROMPT_N=>"%N(%m):%03n:%i> ", :PROMPT_S=>"%N(%m):%03n:%i%l ", :PROMPT_C=>"%N(%m):%03n:%i* ", :RETURN=>"%s\n"}, :SIMPLE=>{:PROMPT_I=>">> ", :PROMPT_N=>">> ", :PROMPT_S=>nil, :PROMPT_C=>"?> ", :RETURN=>"=> %s\n"}, :INF_RUBY=>{:PROMPT_I=>"%N(%m):%03n:%i> ", :PROMPT_N=>nil, :PROMPT_S=>nil, :PROMPT_C=>nil, :RETURN=>"%s\n", :AUTO_INDENT=>true}, :XMP=>{:PROMPT_I=>nil, :PROMPT_N=>nil, :PROMPT_S=>nil, :PROMPT_C=>nil, :RETURN=>" ==>%s\n"}}
CONF[:PROMPT_MODE]=:DEFAULT
CONF[:RC]=true
CONF[:RC_NAME_GENERATOR]=#<Proc:0x007fd1e29e3fc0@/Users/mikemondragon/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/irb/init.rb:268>
CONF[:SAVE_HISTORY]=nil
CONF[:SINGLE_IRB]=false
CONF[:USE_LOADER]=false
CONF[:USE_TRACER]=false
CONF[:VERBOSE]=nil
CONF[:VERSION]="irb 0.9.6(09/06/30)"
% irb
Welcome to IRB. You are using ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-darwin11.4.2]. Have fun ;)
>> IRB.conf
=> CONF[:AP_NAME]="irb"
CONF[:AT_EXIT]=[#<Proc:0x007fa3fa32afc8@/Users/mikemondragon/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/irb/ext/save-history.rb:67>]
CONF[:AUTO_INDENT]=true
CONF[:BACK_TRACE_LIMIT]=16
CONF[:CONTEXT_MODE]=3
CONF[:DEBUG_LEVEL]=1
CONF[:ECHO]=nil
CONF[:EVAL_HISTORY]=42424242424242424242
CONF[:IGNORE_EOF]=false
CONF[:IGNORE_SIGINT]=true
CONF[:INSPECT_MODE]=true
CONF[:IRB_LIB_PATH]="/Users/mikemondragon/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/irb"
CONF[:IRB_NAME]="irb"
CONF[:IRB_RC]=#<Proc:0x007fa3fa2e0f68@/Users/mikemondragon/.rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1/gems/irbtools-1.4.0/lib/irbtools.rb:85>
CONF[:LC_MESSAGES]=#<IRB::Locale:0x007fa3fa03b6c8 @modifier=nil, @encoding_name="UTF-8", @territory="US", @lang="en", @locale="en_US.UTF-8", @encoding=#<Encoding:UTF-8>>
CONF[:LOAD_MODULES]=[]
CONF[:MAIN_CONTEXT]=...myself...
CONF[:MATH_MODE]=false
CONF[:PROMPT]={:NULL=>{:PROMPT_I=>nil, :PROMPT_N=>nil, :PROMPT_S=>nil, :PROMPT_C=>nil, :RETURN=>"%s\n"}, :DEFAULT=>{:PROMPT_I=>"%N(%m):%03n:%i> ", :PROMPT_N=>"%N(%m):%03n:%i> ", :PROMPT_S=>"%N(%m):%03n:%i%l ", :PROMPT_C=>"%N(%m):%03n:%i* ", :RETURN=>"=> %s\n"}, :CLASSIC=>{:PROMPT_I=>"%N(%m):%03n:%i> ", :PROMPT_N=>"%N(%m):%03n:%i> ", :PROMPT_S=>"%N(%m):%03n:%i%l ", :PROMPT_C=>"%N(%m):%03n:%i* ", :RETURN=>"%s\n"}, :SIMPLE=>{:PROMPT_I=>">> ", :PROMPT_N=>">> ", :PROMPT_S=>nil, :PROMPT_C=>"?> ", :RETURN=>"=> %s\n"}, :INF_RUBY=>{:PROMPT_I=>"%N(%m):%03n:%i> ", :PROMPT_N=>nil, :PROMPT_S=>nil, :PROMPT_C=>nil, :RETURN=>"%s\n", :AUTO_INDENT=>true}, :XMP=>{:PROMPT_I=>nil, :PROMPT_N=>nil, :PROMPT_S=>nil, :PROMPT_C=>nil, :RETURN=>" ==>%s\n"}, :IRBTOOLS=>{:PROMPT_I=>">> ", :PROMPT_N=>"| ", :PROMPT_C=>" > ", :PROMPT_S=>"%l> ", :RETURN=>"=> %s \n", :AUTO_INDENT=>true}}
CONF[:PROMPT_MODE]=:IRBTOOLS
CONF[:RC]=true
CONF[:RC_NAME_GENERATOR]=#<Proc:0x007fa3fa189ef8@/Users/mikemondragon/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/irb/init.rb:268>
CONF[:SAVE_HISTORY]=2000
CONF[:SINGLE_IRB]=false
CONF[:USE_LOADER]=false
CONF[:USE_TRACER]=false
CONF[:VERBOSE]=nil
CONF[:VERSION]="irb 0.9.6(09/06/30)"
CONF[:__TMP__EHV__]=...myself...
% irb
irb(main):001:0> IRB.conf
=> CONF[:AP_NAME]="irb"
CONF[:AT_EXIT]=[#<Proc:0x007fd26c1de8b8@/Users/mikemondragon/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/irb/ext/save-history.rb:67>]
CONF[:AUTO_INDENT]=true
CONF[:BACK_TRACE_LIMIT]=16
CONF[:CONTEXT_MODE]=3
CONF[:DEBUG_LEVEL]=1
CONF[:ECHO]=nil
CONF[:EVAL_HISTORY]=nil
CONF[:HISTORY_FILE]="/Users/mikemondragon/.irb-save-history"
CONF[:IGNORE_EOF]=false
CONF[:IGNORE_SIGINT]=true
CONF[:INSPECT_MODE]=true
CONF[:IRB_LIB_PATH]="/Users/mikemondragon/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/irb"
CONF[:IRB_NAME]="irb"
CONF[:IRB_RC]=#<Proc:0x007fd26d0b6070@/Users/mikemondragon/.irbrc:22 (lambda)>
CONF[:LC_MESSAGES]=#<IRB::Locale:0x007fd26c0bcb10 @modifier=nil, @encoding_name="UTF-8", @territory="US", @lang="en", @locale="en_US.UTF-8", @encoding=#<Encoding:UTF-8>>
CONF[:LOAD_MODULES]=[]
CONF[:MAIN_CONTEXT]=...myself...
CONF[:MATH_MODE]=false
CONF[:PROMPT]={:NULL=>{:PROMPT_I=>nil, :PROMPT_N=>nil, :PROMPT_S=>nil, :PROMPT_C=>nil, :RETURN=>"%s\n"}, :DEFAULT=>{:PROMPT_I=>"%N(%m):%03n:%i> ", :PROMPT_N=>"%N(%m):%03n:%i> ", :PROMPT_S=>"%N(%m):%03n:%i%l ", :PROMPT_C=>"%N(%m):%03n:%i* ", :RETURN=>"=> %s\n"}, :CLASSIC=>{:PROMPT_I=>"%N(%m):%03n:%i> ", :PROMPT_N=>"%N(%m):%03n:%i> ", :PROMPT_S=>"%N(%m):%03n:%i%l ", :PROMPT_C=>"%N(%m):%03n:%i* ", :RETURN=>"%s\n"}, :SIMPLE=>{:PROMPT_I=>">> ", :PROMPT_N=>">> ", :PROMPT_S=>nil, :PROMPT_C=>"?> ", :RETURN=>"=> %s\n"}, :INF_RUBY=>{:PROMPT_I=>"%N(%m):%03n:%i> ", :PROMPT_N=>nil, :PROMPT_S=>nil, :PROMPT_C=>nil, :RETURN=>"%s\n", :AUTO_INDENT=>true}, :XMP=>{:PROMPT_I=>nil, :PROMPT_N=>nil, :PROMPT_S=>nil, :PROMPT_C=>nil, :RETURN=>" ==>%s\n"}}
CONF[:PROMPT_MODE]=:DEFAULT
CONF[:RC]=true
CONF[:RC_NAME_GENERATOR]=#<Proc:0x007fd26d170ee8@/Users/mikemondragon/.rbenv/versions/1.9.3-p194/lib/ruby/1.9.1/irb/init.rb:268>
CONF[:SAVE_HISTORY]=10000
CONF[:SINGLE_IRB]=false
CONF[:USE_LOADER]=false
CONF[:USE_TRACER]=false
CONF[:VERBOSE]=nil
CONF[:VERSION]="irb 0.9.6(09/06/30)"
require 'irbtools'
IRB.conf[:AT_EXIT].push proc{ puts "bye bye" }
require 'pp'
# tab-tab method completion on objects
require 'irb/completion'
# bits to save / load 10000 lines of irb history at start/exit
require 'irb/ext/save-history'
IRB.conf[:SAVE_HISTORY] = 10000
IRB.conf[:HISTORY_FILE] = "#{ENV['HOME']}/.irb_history"
# allows editing of commands and other text superpowers
ARGV.concat [ "--readline"]
# or
# IRB.conf[:USE_READLINE] = true
# default/classic prompt are one in the same
ARGV.concat [ "--prompt-mode", "classic" ]
#IRB.conf[:PROMPT_MODE] = :CLASSIC
# simple prompt
#ARGV.concat [ "--simple-prompt"
#IRB.conf[:PROMPT_MODE] = :SIMPLE
# indent at the prompt like an editor
IRB.conf[:AUTO_INDENT] = true
# vim integration
require 'interactive_editor'
# AT_EXIT an array of procs called at exit
IRB.conf[:AT_EXIT].push proc{ puts "bye bye" }

Sets the return value from the last statement evaluated in this context https://github.com/ruby/ruby/blob/trunk/lib/irb/context.rb#L248

module IRB
  class Context
    # The return value of the last statement evaluated.
    attr_reader :last_value

    # Sets the return value from the last statement evaluated in this context
    # to #last_value.
    def set_last_value(value)
      @last_value = value
      @workspace.evaluate self, "_ = IRB.CurrentContext.last_value"
    end
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment