PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > "".to_a isn't consistent with "string".to_a
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
"".to_a isn't consistent with "string".to_a

Réponse
 
LinkBack Outils de la discussion
Vieux 12/03/2008, 03h50   #1
Clifford Heath
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut "".to_a isn't consistent with "string".to_a

I've just been tripped up by this behaviour, which I haven't
seen documented anywhere:

"hello".to_a #=> ["hello"]
"".to_a #=> []

Why does an empty string get omitted from the array?
Why doesn't Enumerable#to_a document this behaviour of strings?
This is completely broken (Ruby 1.8.6)

This affected me in a call to send() using a splat - it always
works ok except when the parameter list is an empty string :-(.

Clifford Heath.
  Réponse avec citation
Vieux 12/03/2008, 04h43   #2
Christopher Swasey
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: "".to_a isn't consistent with "string".to_a

On 3/11/08, Clifford Heath <no@spam.please.net> wrote:
> I've just been tripped up by this behaviour, which I haven't
> seen documented anywhere:
>
> "hello".to_a #=> ["hello"]
> "".to_a #=> []
>
> Why does an empty string get omitted from the array?
> Why doesn't Enumerable#to_a document this behaviour of strings?
> This is completely broken (Ruby 1.8.6)


I disagree. #to_a doesn't just encase the string object in an
otherwise empty array, rather it casts the string to an array, as
appropriate:

"".to_a => []
"test".to_a => ["test"]
"this\nis\na\n\test".to_a => ["this\n", "is\n", "a\n", "test"]

The array equivalent of an empty string is an empty array. Both, for
instance, have a #size of 0.

Christopher

  Réponse avec citation
Vieux 12/03/2008, 04h58   #3
Joel VanderWerf
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: "".to_a isn't consistent with "string".to_a

Christopher Swasey wrote:
> On 3/11/08, Clifford Heath <no@spam.please.net> wrote:
>> I've just been tripped up by this behaviour, which I haven't
>> seen documented anywhere:
>>
>> "hello".to_a #=> ["hello"]
>> "".to_a #=> []
>>
>> Why does an empty string get omitted from the array?
>> Why doesn't Enumerable#to_a document this behaviour of strings?
>> This is completely broken (Ruby 1.8.6)

>
> I disagree. #to_a doesn't just encase the string object in an
> otherwise empty array, rather it casts the string to an array, as
> appropriate:
>
> "".to_a => []
> "test".to_a => ["test"]
> "this\nis\na\n\test".to_a => ["this\n", "is\n", "a\n", "test"]
>
> The array equivalent of an empty string is an empty array. Both, for
> instance, have a #size of 0.


To add to that: the way I've understood it is that, in the context of a
String, the Enumerable methods treat it as an enumeration of lines.

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407

  Réponse avec citation
Vieux 12/03/2008, 05h06   #4
Clifford Heath
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: "".to_a isn't consistent with "string".to_a

Joel VanderWerf wrote:
> To add to that: the way I've understood it is that, in the context of a
> String, the Enumerable methods treat it as an enumeration of lines.


Right, that does make sense now, thanks folk.
Enumerable is built on #each, and String#each does that.

Clifford Heath.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 02h14.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,11453 seconds with 12 queries