Sha256: f1c4d7f955c75c78fa55d54a42ea0d5bcf76ff1d672878297ecb6770ab18ba16

Contents?: true

Size: 903 Bytes

Versions: 1

Compression:

Stored size: 903 Bytes

Contents

module WebConsole
  # Simple read–eval–print implementation.
  #
  # Provides only the most basic code evaluation with no multiline code
  # support.
  class REPL
    # Cleanses exceptions raised inside #send_input.
    cattr_reader :cleaner
    @@cleaner = ActiveSupport::BacktraceCleaner.new
    @@cleaner.add_silencer { |line| line.start_with?(File.expand_path('..', __FILE__)) }

    def initialize(binding = TOPLEVEL_BINDING)
      @binding = binding
    end

    def prompt
      '>> '
    end

    def send_input(input)
      "=> #{@binding.eval(input).inspect}\n"
    rescue Exception => exc
      format_exception(exc)
    end

    private

      def format_exception(exc)
        backtrace = cleaner.clean(Array(exc.backtrace) - caller)

        format = "#{exc.class.name}: #{exc}\n"
        format << backtrace.map { |trace| "\tfrom #{trace}\n" }.join
        format
      end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
web-console-2.0.0 lib/web_console/repl.rb