Skip to content

Commit

Permalink
Fix syntax error messages so that it never shows the underscore varia…
Browse files Browse the repository at this point in the history
…ble.
  • Loading branch information
alloy committed May 30, 2010
1 parent c6b2e0d commit a9ffc8f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 0 additions & 2 deletions TODO
@@ -1,5 +1,3 @@
* Make stdin work
* Fix error message resulting of syntax like: require 'date;
* Make sure the following formatters work: hirb, awesome_print, and looksee
* Make sure the majority of the utils in utility_belt work
* Possibly add copy-paste support as an ext
Expand Down
8 changes: 7 additions & 1 deletion lib/irb/context.rb
Expand Up @@ -34,6 +34,7 @@ def initialize(object, explicit_binding = nil)
@line = 1
clear_buffer

@underscore_assigner = __evaluate__("_ = nil; proc { |val| _ = val }")
@processors = self.class.processors.map { |processor| processor.new(self) }
end

Expand All @@ -42,7 +43,8 @@ def __evaluate__(source, file = __FILE__, line = __LINE__)
end

def evaluate(source)
result = __evaluate__("_ = (#{source})", '(irb)', @line - @source.buffer.size + 1)
result = __evaluate__(source.to_s, '(irb)', @line - @source.buffer.size + 1)
store_result(result)
puts formatter.result(result)
result
rescue Exception => e
Expand Down Expand Up @@ -108,6 +110,10 @@ def formatter
def clear_buffer
@source = Source.new
end

def store_result(result)
@underscore_assigner.call(result)
end
end
end

Expand Down
16 changes: 16 additions & 0 deletions spec/regression/context_spec.rb
@@ -0,0 +1,16 @@
require File.expand_path('../../spec_helper', __FILE__)

main = self

describe "IRB::Context, when evaluating source" do
before do
@context = IRB::Context.new(main)
def @context.printed; @printed ||= '' end
def @context.puts(string); printed << "#{string}\n" end
end

it "does not assign the result to the `_' variable in one go, so it doesn't show up in a syntax error" do
@context.evaluate("'banana;")
@context.printed.should.not.include "_ = ('banana;)"
end
end

0 comments on commit a9ffc8f

Please sign in to comment.