Forum: Ruby strange result from to_a

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
31bb0c6ac470d40572c054ce8a495fa6?d=identicon&s=25 Paul Denize (pdenize)
on 2009-04-10 06:22
I had unexpected results and could not figure out why

puts (" ".."~").to_a.inspect
returns results of " " thru "9"

m = []
(" "[0].."~"[0]).to_a.each{|c| m << c.chr}
puts m.inspect

Is a longwinded way of getting the samne but it gives all the results

" " thru "~"

Can anyone tell me why this is so?

I also hear that to_a is to be depricated - but it is hard to figure out
what the alternatives are.  Everything I found gave the same unexpeted
results.

Paul
797ef431a5e1295b56c08e1db4c8d2df?d=identicon&s=25 botp (Guest)
on 2009-04-10 06:35
(Received via mailing list)
On Fri, Apr 10, 2009 at 12:22 PM, Paul Denize
<paul.denize@datacom.co.nz> wrote:
> I had unexpected results and could not figure out why
>
> puts (" ".."~").to_a.inspect
> returns results of " " thru "9"

works fine here, running ruby1.9
31bb0c6ac470d40572c054ce8a495fa6?d=identicon&s=25 Paul Denize (pdenize)
on 2009-04-10 07:20
Ok, I'm on ruby 1.8.6
Might be time to upgrade.

Thanks
31bb0c6ac470d40572c054ce8a495fa6?d=identicon&s=25 Paul Denize (pdenize)
on 2009-04-10 07:31
Yep, upgraded and the problem is gone.
7a561ec0875fcbbe3066ea8fe288ec77?d=identicon&s=25 Sebastian Hungerecker (Guest)
on 2009-04-10 12:57
(Received via mailing list)
Paul Denize wrote:
> puts (" ".."~").to_a.inspect
> returns results of " " thru "9"

That's because "9".succ is "10" instead of ":"

> I also hear that to_a is to be depricated - but it is hard to figure out
> what the alternatives are.

Only Object#to_a is deprecated.
This topic is locked and can not be replied to.