Last element skip in array mapping

L

Len Lawrence

Still a newbie after 2 years, investigating obscure corners of the
language (version 1.8). I made some tests based on a discussion in
_why's "bits" RedHanded blog:

a = [ 1, 2, 3, 4, 5, nil ]
b = [ 1, 2, 3, 4, 5 ]
x = a.map { |k| k.to_s }.join( "0\n" )
puts x

s = a.map { |k| k }.join( "0\n" )
puts s

z = a.to_a.join( "00\n" )
puts z

beep = b * "000\n"
puts beep

Unless the dummy element is added to the array (as in a) the series does
not complete for any of the mapping methods. Am I missing something here?

Len
 
M

Matt Neuburg

Len Lawrence said:
Still a newbie after 2 years, investigating obscure corners of the
language (version 1.8). I made some tests based on a discussion in
_why's "bits" RedHanded blog:

a = [ 1, 2, 3, 4, 5, nil ]
b = [ 1, 2, 3, 4, 5 ]
x = a.map { |k| k.to_s }.join( "0\n" )
puts x

s = a.map { |k| k }.join( "0\n" )
puts s

z = a.to_a.join( "00\n" )
puts z

beep = b * "000\n"
puts beep

Unless the dummy element is added to the array (as in a) the series does
not complete for any of the mapping methods. Am I missing something here?

Consider:

puts [1].join("0")
puts [1, nil].join("0")

m.
 
L

Len Lawrence

Len Lawrence said:
Still a newbie after 2 years, investigating obscure corners of the
language (version 1.8). I made some tests based on a discussion in
_why's "bits" RedHanded blog:

a = [ 1, 2, 3, 4, 5, nil ]
b = [ 1, 2, 3, 4, 5 ]
x = a.map { |k| k.to_s }.join( "0\n" ) puts x

s = a.map { |k| k }.join( "0\n" )
puts s

z = a.to_a.join( "00\n" )
puts z

beep = b * "000\n"
puts beep

Unless the dummy element is added to the array (as in a) the series
does not complete for any of the mapping methods. Am I missing
something here?

Consider:

puts [1].join("0")
puts [1, nil].join("0")

m.

Thanks. Back to the documentation.
Misunderstanding - thought join meant adding the argument string to the
array element. I see that the argument is a separator between elements.
Duh! Easy to miss these things at my age (70).

Len
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top