[macruby-changes] [2241] MacRuby/trunk/lib/stringio.rb

source_changes at macosforge.org source_changes at macosforge.org
Thu Aug 6 02:18:42 PDT 2009


Revision: 2241
          http://trac.macosforge.org/projects/ruby/changeset/2241
Author:   mattaimonetti at gmail.com
Date:     2009-08-06 02:18:40 -0700 (Thu, 06 Aug 2009)
Log Message:
-----------
Merge branch 'stringio'

* stringio:
  added #gets and #getline to StringIO
  added each_char and chars

Modified Paths:
--------------
    MacRuby/trunk/lib/stringio.rb

Modified: MacRuby/trunk/lib/stringio.rb
===================================================================
--- MacRuby/trunk/lib/stringio.rb	2009-08-06 08:41:23 UTC (rev 2240)
+++ MacRuby/trunk/lib/stringio.rb	2009-08-06 09:18:40 UTC (rev 2241)
@@ -315,6 +315,16 @@
   # See IO#each_byte.
   #
   def each_byte
+  end
+  
+  #   strio.gets(sep=$/)     -> string or nil
+  #   strio.gets(limit)      -> string or nil
+  #   strio.gets(sep, limit) -> string or nil
+  #
+  # See IO#gets.
+  #
+  def gets(sep=$/)
+    $_ = self.getline(sep)
   end 
            
 
@@ -385,5 +395,40 @@
       @append = true if (mode & IO::APPEND) != 0
       @string.replace("") if (mode & IO::TRUNC) != 0
     end
+    
+    def getline(sep = $/)
+      raise(IOError, "not opened for reading") unless @readable
+      sep = sep.to_str unless (sep.nil? || sep.kind_of?(String))
+      return nil if self.eof?
 
+      if sep.nil?
+        line = string[pos .. -1]
+        @pos = string.size
+      elsif sep.empty?
+        if stop = string.index("\n\n", pos)
+          stop += 2
+          line = string[pos .. (stop - 2)]
+          while string[stop] == ?\n
+            stop += 1
+          end
+          @pos = stop
+        else
+          line = string[pos .. -1]
+          @pos = string.size
+        end
+      else
+        if stop = string.index(sep, pos)
+          line = string[pos .. stop]
+          @pos = stop + 1
+        else
+          line = string[pos .. -1]
+          @pos = string.size
+        end
+      end
+
+      @lineno += 1
+
+      return line
+    end  
+
 end
\ No newline at end of file
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090806/690e3459/attachment.html>


More information about the macruby-changes mailing list