Note the use of a , rather than a ; between the | ... |
Also why are you assigning an Array to newstr? It makes absolutely no sense
as x.capitalize will simply replace it with a string.
The use of , rather than ; holds for all your examples.
This has nothing to do with creating local variables and everything to do
with writing bad code.
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.
Can anyone tell me what's the wrong? Does the author said wrong or my
caught is wrong?
TypeError: can't convert nil into String
from (irb):2
from C:/Ruby193/bin/irb:12:in `<main>'
Got the above one.
Well. `a = "hello world".split(//).each{ |x| newstr << x.capitalize }`
is running on your IRB. I think it should not.
a = "hello world".split(//).each{ |x| newstr << x.capitalize }
NameError: undefined local variable or method `newstr' for main:Object
from (irb):3:in `block in irb_binding'
from (irb):3:in `each'
from (irb):3
from C:/Ruby193/bin/irb:12:in `<main>'
The error is so obvious. But don't know why author choose this code to
explain.
No, please look at the symbol `;`, which is used to declare block local
variables with the block parameters inside the `|`.
[1,2,3].each {}
=> [1, 2, 3]
[1,2,3].each {|x;y| print y}
=> [1, 2, 3]
[1,2,3].each {|x;y| y=x; print y}
123=> [1, 2, 3]
See above. Here `x` is block parameter and `y` is local variable to the
block. The only problem if you try to get that `y` to initialize in the
same place inside the `|`. - Why so?
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 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!
···
Am 11.03.2013 13:22, schrieb Love U Ruby:
Can anyone tell me what's the wrong? Does the author said wrong or my
caught is wrong?