From: Quintus Date: 2010-10-04T04:12:31+09:00 Subject: Re: Puts to stream? -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Am 03.10.2010 20:54, schrieb Terry Michaels: > I've got simple statements like so: > > puts "hi" > puts "there" > puts name > > Wanted to adjust it to write to IO object (a string) instead of stdout. > This is the kind of construction I'd like to use: > > my_out = String.new > puts my_out "hi" > puts my_out "there" > puts my_out name > > But apparently that doesn't work. This seems to work > > my_out << "hi" << "\n" > my_out << "there" << "\n" > my_out << name << "\n" > > The lame part of that, though, is that it does not do the whole "check > if there is an end-line and add one if there isn't" magic, so I have to > manually specify the newline if I want one. I tried > > my_out.puts "hi" > > But evidently that does the exact opposite of want I want. Maybe use a StringIO? - ------------------------------- require "stringio" str = StringIO.new("") str.puts("hi") str.puts("there") puts str.string #=> "hi\nthere\n" - ------------------------------- Vale, Marvin -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.10 (GNU/Linux) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iQEcBAEBAgAGBQJMqNWWAAoJEGrS0YjAWTKVqRwH/1+fbPjc756bpCJr2T6dRtas I83rVh/CLmEQkmnCq+S8Lg7yJNay+vAyhxJo0nACfNk8pUfPcAiZI/oaTsb/7Edx d7bLweNwuC9u2J2s2s8RB3tiQc1zkZRVrzZb8vzTBX6DX/h5XJXnI+SDM/RmPr9E DwHaKNj+4Pr3DPS6DCfxkmznTIsEFUodNT76KhsoUlR+dwbPCC8PMxhcSqRoM0no mpctqCDqfWbw0981ebtIJgixp1bMMaSRHzatk+H2HnMtLDkdx4v/HInQ67xT+BkK x7Z/xooAPs916KQ/xMX2l+lE4hYnCqFsW0t3mCDrYN+T7mS0dySHfqeBXRKeuqI= =WRu4 -----END PGP SIGNATURE-----