[macruby-changes] [4597] DietRB/trunk/lib/irb/source.rb

source_changes at macosforge.org source_changes at macosforge.org
Fri Oct 8 03:46:14 PDT 2010


Revision: 4597
          http://trac.macosforge.org/projects/ruby/changeset/4597
Author:   eloy.de.enige at gmail.com
Date:     2010-10-08 03:46:13 -0700 (Fri, 08 Oct 2010)
Log Message:
-----------
Cleanup and docs.

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

Modified Paths:
--------------
    DietRB/trunk/lib/irb/source.rb

Modified: DietRB/trunk/lib/irb/source.rb
===================================================================
--- DietRB/trunk/lib/irb/source.rb	2010-10-08 10:46:05 UTC (rev 4596)
+++ DietRB/trunk/lib/irb/source.rb	2010-10-08 10:46:13 UTC (rev 4597)
@@ -2,30 +2,6 @@
 
 module IRB
   class Source
-    class Reflector < Ripper::SexpBuilder
-      attr_reader :level
-      
-      def initialize(source)
-        super
-        @level = 0
-        @valid = !parse.nil?
-      end
-      
-      def valid?
-        @valid
-      end
-      
-      def on_kw(token)
-        case token
-        when "class", "def"
-          @level += 1
-        when "end"
-          @level -= 1
-        end
-        super
-      end
-    end
-    
     attr_reader :buffer
     
     def initialize(buffer = [])
@@ -38,30 +14,66 @@
       @buffer << source.chomp
     end
     
+    # Returns the accumulated source as a string, joined by newlines.
     def source
       @buffer.join("\n")
     end
     
+    # Reflects on the accumulated source and returns the current code block
+    # indentation level.
     def level
       reflect.level
     end
     
-    # This does not take syntax errors in account, but only whether or not the
-    # accumulated source up till now is a valid code block.
-    #
-    # For example, this is not a valid full code block:
-    #
-    #   def foo; p :ok
-    #
-    # This however is:
-    #
-    #   def foo; p :ok; end
+    # Reflects on the accumulated source to see if it's a valid code block.
     def valid?
       reflect.valid?
     end
     
+    # Returns a Reflector for the accumulated source and caches it.
     def reflect
       @reflection ||= Reflector.new(source)
     end
+    
+    class Reflector < Ripper::SexpBuilder
+      def initialize(source)
+        super
+        @level = 0
+        @valid = !parse.nil?
+      end
+      
+      # Returns the code block indentation level.
+      #
+      #   Reflector.new("").level # => 0
+      #   Reflector.new("class Foo").level # => 1
+      #   Reflector.new("class Foo; def foo").level # => 2
+      #   Reflector.new("class Foo; def foo; end").level # => 1
+      #   Reflector.new("class Foo; def foo; end; end").level # => 0
+      attr_reader :level
+      
+      # Returns whether or not the source is a valid code block, but does not
+      # take syntax errors into account.
+      #
+      # For example, this is not a valid full code block:
+      #
+      #   def foo; p :ok
+      #
+      # This however is:
+      #
+      #   def foo; p :ok; end
+      def valid?
+        @valid
+      end
+      
+      def on_kw(token) #:nodoc:
+        case token
+        when "class", "def"
+          @level += 1
+        when "end"
+          @level -= 1
+        end
+        super
+      end
+    end
   end
 end
\ No newline at end of file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20101008/bf8d456a/attachment.html>


More information about the macruby-changes mailing list