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 > Re: shortcut for x = [x] unless x.is_a?(Array)
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Re: shortcut for x = [x] unless x.is_a?(Array)

Réponse
 
LinkBack Outils de la discussion
Vieux 10/03/2008, 23h57   #1
Siep Korteling
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shortcut for x = [x] unless x.is_a?(Array)

Aryk Grosz wrote:
> Is there any prettier or cleaner way to write
>
> x = [x] unless x.is_a?(Array)


x = x.to_a

Or am I overseeing something?

Regards,

Siep



--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 11/03/2008, 00h11   #2
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shortcut for x = [x] unless x.is_a?(Array)

On Mon, Mar 10, 2008 at 5:57 PM, Siep Korteling <s.korteling@gmail.com> wrote:
> Aryk Grosz wrote:
> > Is there any prettier or cleaner way to write
> >
> > x = [x] unless x.is_a?(Array)

>
> x = x.to_a
>
> Or am I overseeing something?
>
> Regards,
>
> Siep


#to_a can behave differently

x = {}
x = x.to_a
=> [x]

x = {}
x = [x] unless x.is_a?(Array)
=> [{x}]

x = 5
x.to_a
=> some warning about default to_a being deprecated

Todd

  Réponse avec citation
Vieux 11/03/2008, 00h16   #3
Paul Mckibbin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shortcut for x = unless x.is_a?(Array)


> => some warning about default to_a being deprecated
>
> Todd


which is why x=Array(x) would be better.

Mac
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 11/03/2008, 00h19   #4
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shortcut for x = [x] unless x.is_a?(Array)

On Mon, Mar 10, 2008 at 6:11 PM, Todd Benson <caduceass@gmail.com> wrote:
> On Mon, Mar 10, 2008 at 5:57 PM, Siep Korteling <s.korteling@gmail.com> wrote:
> > Aryk Grosz wrote:
> > > Is there any prettier or cleaner way to write
> > >
> > > x = [x] unless x.is_a?(Array)

> >
> > x = x.to_a
> >
> > Or am I overseeing something?
> >
> > Regards,
> >
> > Siep

>
> #to_a can behave differently
>
> x = {}
> x = x.to_a
> => [x]
>
> x = {}
>
> x = [x] unless x.is_a?(Array)
> => [{x}]
>
> x = 5
> x.to_a
> => some warning about default to_a being deprecated


I should add, too, that Array[] is different than Array().

Todd

  Réponse avec citation
Vieux 11/03/2008, 03h03   #5
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shortcut for x = unless x.is_a?(Array)

On Mon, Mar 10, 2008 at 6:16 PM, Paul Mckibbin <pmckibbin@gmail.com> wrote:
>
> > => some warning about default to_a being deprecated
> >
> > Todd

>
> which is why x=Array(x) would be better.
>
> Mac


Like your original response, it depends on what you want to do.

x = {}
Array[x] == Array(x)
=> false

In this case -- with the result -- one encapsulates, the other
modifies. Same thing with NilClass and Range. I added a line to your
code to see this for sure...

TEST=[[1,2,3],(1..3),{:a=>:b},1,"test",true,nil,/123/]

TEST.each do |x|
puts x.class
puts '============================'
puts "#{x.inspect}.to_a gives #{x.to_a.inspect}"
puts "[#{x.inspect}] gives #{([x]).inspect}"
puts "Array(#{x.inspect}) gives #{Array(x).inspect}"
puts "Array[#{x.inspect}] gives #{Array[x].inspect}"
#added this ^^^^^^ one
puts '============================'
end

Todd

  Réponse avec citation
Vieux 11/03/2008, 11h30   #6
Paul Mckibbin
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shortcut for x = unless x.is_a?(Array)


> puts "Array[#{x.inspect}] gives #{Array[x].inspect}"
> #added this ^^^^^^ one
> puts '============================'
> end
>
> Todd


Hi Todd,

Array[x] is equivalent to [x].

Mac
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 11/03/2008, 17h37   #7
Todd Benson
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shortcut for x = unless x.is_a?(Array)

On Tue, Mar 11, 2008 at 5:30 AM, Paul Mckibbin <pmckibbin@gmail.com> wrote:
>
> > puts "Array[#{x.inspect}] gives #{Array[x].inspect}"
> > #added this ^^^^^^ one
> > puts '============================'
> > end
> >
> > Todd

>
> Hi Todd,
>
> Array[x] is equivalent to [x].


Hi,

I know that now. Just wanted to make sure. Thanks.

Todd

  Réponse avec citation
Vieux 12/03/2008, 02h29   #8
Nikhil Warade
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: shortcut for x = unless x.is_a?(Array)

Paul Mckibbin wrote:
>
>> => some warning about default to_a being deprecated
>>
>> Todd

>
> which is why x=Array(x) would be better.
>
> Mac


(1..3).to_a will not give any warning and will convert it to [1,2,3]
--
Posted via http://www.ruby-forum.com/.

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


Édité par : vBulletin® version 3.7.2
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,11554 seconds with 16 queries