[macruby-changes] [4593] DietRB/trunk

source_changes at macosforge.org source_changes at macosforge.org
Fri Oct 8 03:45:38 PDT 2010


Revision: 4593
          http://trac.macosforge.org/projects/ruby/changeset/4593
Author:   eloy.de.enige at gmail.com
Date:     2010-10-08 03:45:37 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Initial commit

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

Added Paths:
-----------
    DietRB/trunk/README
    DietRB/trunk/napkin.rb

Added: DietRB/trunk/README
===================================================================
--- DietRB/trunk/README	                        (rev 0)
+++ DietRB/trunk/README	2010-10-08 10:45:37 UTC (rev 4593)
@@ -0,0 +1 @@
+IRB on a diet, for MacRuby / Ruby 1.9
\ No newline at end of file

Added: DietRB/trunk/napkin.rb
===================================================================
--- DietRB/trunk/napkin.rb	                        (rev 0)
+++ DietRB/trunk/napkin.rb	2010-10-08 10:45:37 UTC (rev 4593)
@@ -0,0 +1,68 @@
+#!/usr/bin/env macruby
+
+require 'readline'
+require 'ripper'
+
+module IRB
+  class << self
+    def start
+      Context.new.run
+    end
+  end
+  
+  class Context
+    def initialize
+      @line = 1
+      @source = ""
+    end
+    
+    def run
+      while @source << gets
+        exit if @source == "quit\n"
+        if valid_code_block?
+          eval
+          @source = ""
+        end
+      end
+    end
+    
+    def gets
+      result = "#{Readline.readline(prompt, true)}\n"
+      @line += 1
+      result
+    end
+    
+    def eval
+      print_result(super(@source, TOPLEVEL_BINDING))
+    rescue Object => e
+      print_exception(e)
+    end
+    
+    # we need to print the level, which means we need to do actual parsing
+    #
+    # eg:
+    #
+    #   irb(main):001:0> class A
+    #   irb(main):002:1> def foo
+    #   irb(main):003:2> p :ok
+    #   irb(main):004:2> end
+    #   irb(main):005:1> end
+    def prompt
+      "irb(main):00#{@line}:0> "
+    end
+    
+    def print_result(output)
+      puts "=> #{output.inspect}"
+    end
+    
+    def print_exception(e)
+      puts "#{e.class.name}: #{e.message}\n\t#{e.backtrace.join("\n\t")}"
+    end
+    
+    def valid_code_block?
+      !!Ripper::SexpBuilder.new(@source).parse
+    end
+  end
+end
+
+IRB.start
\ No newline at end of file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101008/5cbef6d3/attachment-0001.html>


More information about the macruby-changes mailing list