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

source_changes at macosforge.org source_changes at macosforge.org
Fri Aug 7 14:13:53 PDT 2009


Revision: 2260
          http://trac.macosforge.org/projects/ruby/changeset/2260
Author:   mattaimonetti at gmail.com
Date:     2009-08-07 14:13:53 -0700 (Fri, 07 Aug 2009)
Log Message:
-----------
added #print and #printf to StringIO

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

Modified: MacRuby/trunk/lib/stringio.rb
===================================================================
--- MacRuby/trunk/lib/stringio.rb	2009-08-07 21:13:48 UTC (rev 2259)
+++ MacRuby/trunk/lib/stringio.rb	2009-08-07 21:13:53 UTC (rev 2260)
@@ -531,7 +531,7 @@
   
   #   strio.puts(obj, ...)    -> nil
   #
-  #  Writes the given objects to <em>ios</em> as with
+  #  Writes the given objects to <em>strio</em> as with
   #  <code>IO#print</code>. Writes a record separator (typically a
   #  newline) after any that do not already end with a newline sequence.
   #  If called with an array argument, writes each element on a new line.
@@ -569,9 +569,51 @@
     end
     
     nil
-  end           
+  end
+  
+  #     strio.print()             => nil
+  #     strio.print(obj, ...)     => nil
+  #
+  #  Writes the given object(s) to <em>strio</em>. The stream must be
+  #  opened for writing. If the output record separator (<code>$\\</code>)
+  #  is not <code>nil</code>, it will be appended to the output. If no
+  #  arguments are given, prints <code>$_</code>. Objects that aren't
+  #  strings will be converted by calling their <code>to_s</code> method.
+  #  With no argument, prints the contents of the variable <code>$_</code>.
+  #  Returns <code>nil</code>.
+  #
+  #     io.print("This is ", 100, " percent.\n")
+  #
+  #  <em>produces:</em>
+  #
+  #     This is 100 percent.
+  #
+  def print(*args)
+    raise IOError, "not opened for writing" unless @writable
+    args << $_ if args.empty?
+    args.map! { |x| (x == nil) ? "nil" : x }
+    write((args << $\).flatten.join)
+    nil
+  end 
+  
+  #     printf(strio, string [, obj ... ] )    => nil
+  #
+  #  Equivalent to:
+  #     strio.write(sprintf(string, obj, ...)
+  #
+  def printf(*args)
+    raise IOError, "not opened for writing" unless @writable
 
+    if args.size > 1
+      write(args.shift % args)
+    else
+      write(args.first)
+    end
 
+    nil
+  end          
+
+
   protected
     
     # meant to be overwritten by developers
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/macruby-changes/attachments/20090807/18fba6ba/attachment-0001.html>


More information about the macruby-changes mailing list