Sha256: d148430b3d02b86c118053443bf67772350933504d6a5d18cb8fae22ff970589

Contents?: true

Size: 1.47 KB

Versions: 18

Compression:

Stored size: 1.47 KB

Contents

module Redcar
  class REPL
    class RubyMirror
      include Redcar::REPL::ReplMirror
      
      def initialize
        # required by ReplMirror
        @prompt = ">>"
	
        @history = "# Redcar REPL\n\n#{@prompt} "
        @instance = Main.new
      end

      def title
        "Ruby REPL"
      end
      
      def grammar_name
        "Ruby REPL"
      end
      
      # Get the complete history as a pretty formatted string.
      #
      # @return [String]
      def read
        @history
      end
      
      def clear_history
        @history = @history.split("\n").last
        notify_listeners(:change)
      end

      private
      
      class Main
        attr_reader :output
        
        def initialize
          @binding = binding
          @output = nil
        end

        def inspect
          "main"
        end
        
        def execute(command)
          eval(command, @binding).inspect
        end
      end
      
      def format_error(e)
        backtrace = e.backtrace.reject{|l| l =~ /ruby_mirror/}
        backtrace.unshift("(repl):1")
        "#{e.class}: #{e.message}\n        #{backtrace.join("\n        ")}"
      end
      
      def send_to_repl expr
        @history += expr + "\n"
        begin
          @history += "=> " + @instance.execute(expr)
        rescue Object => e
          @history += "x> " + format_error(e)
        end
        @history += "\n" + @prompt + " "
        notify_listeners(:change)
      end
            
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
redcar-0.7 plugins/repl/lib/repl/ruby_mirror.rb
redcar-0.6.1 plugins/repl/lib/repl/ruby_mirror.rb
redcar-0.6 plugins/repl/lib/repl/ruby_mirror.rb
redcar-0.6.1dev plugins/repl/lib/repl/ruby_mirror.rb
redcar-0.5.1 plugins/repl/lib/repl/ruby_mirror.rb
redcar-0.5 plugins/repl/lib/repl/ruby_mirror.rb
redcar-0.5.6dev plugins/repl/lib/repl/ruby_mirror.rb
redcar-0.5.5dev plugins/repl/lib/repl/ruby_mirror.rb
redcar-0.5.4dev plugins/repl/lib/repl/ruby_mirror.rb
redcar-0.5.3dev plugins/repl/lib/repl/ruby_mirror.rb
redcar-0.5.2dev plugins/repl/lib/repl/ruby_mirror.rb
redcar-0.5.1dev plugins/repl/lib/repl/ruby_mirror.rb
redcar-0.4.1 plugins/repl/lib/repl/ruby_mirror.rb
redcar-0.4 plugins/repl/lib/repl/ruby_mirror.rb
redcar-0.3.10.1dev plugins/repl/lib/repl/ruby_mirror.rb
redcar-0.3.10.0dev plugins/repl/lib/repl/ruby_mirror.rb
redcar-0.3.9 plugins/repl/lib/repl/ruby_mirror.rb
redcar-0.3.9.0dev plugins/repl/lib/repl/ruby_mirror.rb