Class: MimickIRB
- Inherits:
-
RubyLex
- Object
- RubyLex
- MimickIRB
- Defined in:
- lib/shoes/shoes_irb.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#echo ⇒ Object
Returns the value of attribute echo.
-
#history ⇒ Object
Returns the value of attribute history.
-
#started ⇒ Object
Returns the value of attribute started.
Instance Method Summary collapse
-
#initialize ⇒ MimickIRB
constructor
A new instance of MimickIRB.
- #run(str) ⇒ Object
Constructor Details
Instance Attribute Details
#echo ⇒ Object
Returns the value of attribute echo.
5 6 7 |
# File 'lib/shoes/shoes_irb.rb', line 5 def echo @echo end |
#history ⇒ Object
Returns the value of attribute history.
5 6 7 |
# File 'lib/shoes/shoes_irb.rb', line 5 def history @history end |
#started ⇒ Object
Returns the value of attribute started.
5 6 7 |
# File 'lib/shoes/shoes_irb.rb', line 5 def started @started end |
Instance Method Details
#run(str) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/shoes/shoes_irb.rb', line 19 def run(str) obj = nil @io << str @io.rewind unless l = lex raise Empty if @line == '' else case l.strip when "reset" @line = "" when "time" @line = "puts %{This IRB session started #{Time.at(Time.now.getutc - @started.getutc.to_f).strftime('%H:%M:%S')} ago.}" when /^echo( (.*))?/ case $2 when "on" @echo = true @line = "puts %{echo is toggled on}" when "off" @echo = false @line = "puts %{echo is toggled off}" else @line = "puts %{echo is toggled #{@echo ? "on" : "off"}}" end when "history" @line = "puts %{#{@history.join("\n")}}" when "save" filename = ask_save_file if filename File.open(filename, "w") { |f| f.write(@history.join("\n")) } @line = "puts %{Command history saved to #{filename.inspect}...}" else @line = "" end when "help" = <<-END.gsub(/^ {12}/, '') help\tdisplay this help. echo\techo, echo on and echo off to toggle object output. time\tdisplay time since the IRB session started. history\tlist command history. save\tsave command history to a file. reset\treset terminal. END @line = "puts \"#{}\"" else @line << l << "\n" if @ltype or @continue or @indent > 0 raise Continue end end end unless @line.empty? obj = eval @line, TOPLEVEL_BINDING, "(irb)", @line_no end @line_no += @line.scan(/\n/).length @line = '' @exp_line_no = @line_no @indent = 0 @indent_stack = [] $stdout.rewind output = $stdout.read $stdout.truncate(0) $stdout.rewind [output, obj] rescue Object => e case e when Empty, Continue else @line = "" end raise e ensure set_input(StringIO.new) end |