-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 On May 16, 2008, at 8:38 PM, Jason Lillywhite wrote: > class NameIt > def initialize(name) > @name = name > end > def to_s > puts "My name is: " << @name > end > end > names = %w( tom sally doug john) > nameits = names.map {|name| NameIt.new(name)} > nameits.each{|n| puts n.to_s} > > and it returned this: >> > My name is: tom > nil > My name is: sally > nil > My name is: doug > nil > My name is: john > nil >> Exit code: 0 > > Where did the nil come from? To give you a more elaborate answer on why "nil" is printed... Lets isolate that case: === def foo puts "bar" end puts foo === This prints bar nil Why? The Method #puts prints to standard out _and returns nil_. (in Ruby 1.8.6, that will change in 1.9). Every statement and method in Ruby has a return value. In the case of Methods, the return value is eighter explicitly stated or the value of the last statement in the method body. So the #foo returns nil. As you call #puts again on the return value of foo, you get the string representation of nil. Which is... "nil". So you essentially wrote: puts(puts("bar")) Regards, Florian Gilcher -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (Darwin) iEYEARECAAYFAkgt7iQACgkQJA/zY0IIRZZclwCfdm/QO25t7fQyj8gbWbgbeJBK +lwAnA5EVgaQlGlD996qcme8eZY9h3xt =wVhz -----END PGP SIGNATURE-----