[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Last element skip in array mapping

Len Lawrence

6/13/2009 3:56:00 PM

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


2 Answers

matt_neuburg

6/13/2009 4:52:00 PM

0

Len Lawrence <lcl@tarazed.demon.co.uk> wrote:

> 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.


Len Lawrence

6/13/2009 5:19:00 PM

0

On Sat, 13 Jun 2009 09:52:17 -0700, Matt Neuburg wrote:

> Len Lawrence <lcl@tarazed.demon.co.uk> wrote:
>
>> 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