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

source_changes at macosforge.org source_changes at macosforge.org
Thu Aug 6 22:41:47 PDT 2009


Revision: 2251
          http://trac.macosforge.org/projects/ruby/changeset/2251
Author:   mattaimonetti at gmail.com
Date:     2009-08-06 22:41:46 -0700 (Thu, 06 Aug 2009)
Log Message:
-----------
added #write #syswrite #<< and #close_write to StringIO

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

Modified: MacRuby/trunk/lib/stringio.rb
===================================================================
--- MacRuby/trunk/lib/stringio.rb	2009-08-07 04:23:31 UTC (rev 2250)
+++ MacRuby/trunk/lib/stringio.rb	2009-08-07 05:41:46 UTC (rev 2251)
@@ -335,6 +335,51 @@
   def readline(sep=$/)
     raise(IO::EOFError, 'end of file reached') if eof?
     $_ = getline(sep)
+  end
+  
+  
+  #   strio.write(string)    -> integer
+  #   strio.syswrite(string) -> integer
+  #
+  # Appends the given string to the underlying buffer string of *strio*.
+  # The stream must be opened for writing.  If the argument is not a
+  # string, it will be converted to a string using <code>to_s</code>.
+  # Returns the number of bytes written.  See IO#write.
+  #
+  def write(str)
+    str = str.to_s unless str.kind_of?(String)
+    return 0 if str.empty?
+
+    raise(IOError, "not opened for writing") unless @writable    
+ 
+    if @append || (pos >= string.length)
+      # add padding in case it's needed 
+      str = str.rjust((pos + str.length) - string.length, "\000") if (pos > string.length)
+      @string << str
+      @pos = string.length
+    else
+      @string[pos, str.length] = str
+      @pos += str.length
+      @string.taint if str.tainted?
+    end
+ 
+    str.length
+  end
+  alias_method :syswrite, :write
+  
+  #   strio << obj     -> strio
+  #
+  # See IO#<<.
+  #
+  def <<(str)
+    self.write(str)
+    self
+  end
+
+  
+  def close_write
+    raise(IOError, "closing non-duplex IO for writing") unless @writable
+    @writable = false
   end 
            
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090806/80c60b12/attachment.html>


More information about the macruby-changes mailing list