[macruby-changes] [4607] DietRB/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Oct 8 03:47:49 PDT 2010


Revision: 4607
          http://trac.macosforge.org/projects/ruby/changeset/4607
Author:   eloy.de.enige at gmail.com
Date:     2010-10-08 03:47:48 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Print the result of evaluating, or an exception if it occurs. Also store the result in a local variable called `_', as IRB does.

From: Eloy Duran <eloy.de.enige at gmail.com>

Modified Paths:
--------------
    DietRB/trunk/lib/irb/context.rb
    DietRB/trunk/spec/context_spec.rb

Modified: DietRB/trunk/lib/irb/context.rb
===================================================================
--- DietRB/trunk/lib/irb/context.rb	2010-10-08 10:47:38 UTC (rev 4606)
+++ DietRB/trunk/lib/irb/context.rb	2010-10-08 10:47:48 UTC (rev 4607)
@@ -12,9 +12,11 @@
     end
     
     def evaluate(source)
-      eval(source.to_s, @binding)
+      result = eval("_ = (#{source})", @binding)
+      puts "=> #{result.inspect}"
+      result
     rescue Exception => e
-      
+      puts format_exception(e)
     end
     
     def readline
@@ -41,6 +43,10 @@
       PROMPT % [@object.inspect, @line, @source.level]
     end
     
+    def format_exception(e)
+      "#{e.class.name}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
+    end
+    
     private
     
     def clear_buffer

Modified: DietRB/trunk/spec/context_spec.rb
===================================================================
--- DietRB/trunk/spec/context_spec.rb	2010-10-08 10:47:38 UTC (rev 4606)
+++ DietRB/trunk/spec/context_spec.rb	2010-10-08 10:47:48 UTC (rev 4607)
@@ -39,17 +39,36 @@
     o = Object.new
     IRB::Context.new(o).prompt.should == "irb(#{o.inspect}):001:0> "
   end
+  
+  it "returns a formatted exception message" do
+    begin; DoesNotExist; rescue NameError => e; exception = e; end
+    @context.format_exception(exception).should ==
+      "NameError: uninitialized constant Bacon::Context::DoesNotExist\n\t#{exception.backtrace.join("\n\t")}"
+  end
 end
 
 describe "IRB::Context, when evaluating source" do
   before do
     @context = IRB::Context.new(main)
+    def @context.puts(string); @printed = string; end
   end
   
   it "evaluates code with the object's binding" do
     @context.evaluate("self").should == main
   end
   
+  it "prints the result" do
+    @context.evaluate("Hash[:foo, :foo]")
+    printed = @context.instance_variable_get(:@printed)
+    printed.should == "=> {:foo=>:foo}"
+  end
+  
+  it "assigns the result to the local variable `_'" do
+    result = @context.evaluate("Object.new")
+    @context.evaluate("_").should == result
+    @context.evaluate("_").should == result
+  end
+  
   it "coerces the given source to a string first" do
     o = Object.new
     def o.to_s; "self"; end
@@ -62,6 +81,12 @@
       @context.evaluate("raise Exception")
     }.should.not.raise
   end
+  
+  it "prints the exception that occurs" do
+    @context.evaluate("DoesNotExist")
+    printed = @context.instance_variable_get(:@printed)
+    printed.should.match /^NameError: uninitialized constant DoesNotExist/
+  end
 end
 
 class << Readline
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101008/f647ab95/attachment.html>


More information about the macruby-changes mailing list