From: sto.mar@... Date: 2013-03-12T04:59:09+09:00 Subject: Re: Confusion with block local variable declaration with block variable declaration within the pipe Am 11.03.2013 13:22, schrieb Love U Ruby: > In the "Book of Ruby" author wrote the below paragraph: > > a = "hello world".split(//).each{ |x| newstr << x.capitalize } (<~~ But > this code is not working in my `IRB`. And I think it should not. As > `newstr` is not initialized before it's used. - Is this the reason for > the code not to work.) > > So, at each iteration, a capitalized character is appended to newstr, > and the following is displayed... > > H > HE > HEL > HELL > HELLO > HELLO > HELLO W > HELLO WO > HELLO WOR > HELLO WORL > HELLO WORLD > > As we are using the capitalize method here (with no terminating ! > character), the characters in the array, a, remain as they began, all > lowercase, since the capitalize method does not alter the receiver > object (here the receiver objects are the characters passed into the > block). Be aware, however, that this code would not work if you were to > use the capitalize! method to modify the original characters. ** This is > because capitalize! returns nil when no changes are made so when the > space character is encountered nil would be returned and our attempt to > append (<<) a nil value to the string, newstr, would fail.** > > I didn't catch him with ***points. > >>> " ".capitalize! > => nil >>> a =" ".capitalize! > => nil >>> a > => nil >>> a= 12 > => 12 >>> a =" ".capitalize! > => nil >>> a > => nil > > No error I did encounter. Because you did not try to append nil to a string! > Can anyone tell me what's the wrong? Does the author said wrong or my > caught is wrong? --