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 > bug with assignment?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
bug with assignment?

Réponse
 
LinkBack Outils de la discussion
Vieux 16/09/2007, 19h23   #1
Mukund
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut bug with assignment?

The snippet illustrates my question.

a={}
b={}
a["alpha"]=b["alpha"]=[]
a["alpha"]<<23
b["alpha"]<<100
puts a["alpha"]
puts b["alpha"]

Here a["alpha"] and b["alpha"] both contain an array of [23,100].
Why??

If I remove the multiple assignment,
a={}
b={}
a["alpha"]=[]
b["alpha"]=[]
a["alpha"]<<23
b["alpha"]<<100
puts a["alpha"]
puts b["alpha"]


a["alpha"] contains 23 and b["alpha"] contains 100 as I expected.

  Réponse avec citation
Vieux 16/09/2007, 19h40   #2
Ben Bleything
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bug with assignment?

On Mon, Sep 17, 2007, Mukund wrote:
> Here a["alpha"] and b["alpha"] both contain an array of [23,100].
> Why??


It's this line, right here.

> a["alpha"]=b["alpha"]=[]


What's happening is the a["alpha"] is getting the result of
b["alpha"]=[], which is the empty array on the far-right of the
assignment. Both a["alpha"] and b["alpha"] contain a reference to the
same array.

> If I remove the multiple assignment,
> a["alpha"]=[]
> b["alpha"]=[]


a["alpha"] and b["alpha"] now are both references to *different* empty
arrays.

Hope that s

Ben

  Réponse avec citation
Vieux 16/09/2007, 19h45   #3
Tim Pease
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bug with assignment?

On 9/16/07, Mukund <maruthim@yahoo.com> wrote:
> The snippet illustrates my question.
>
> a={}
> b={}
> a["alpha"]=b["alpha"]=[]


You are assigning a new Array to the key "alpha" in the hash b. The
return value from hash assignment is the object stored in the hash --
i.e. the new Array you created. This object is then assigned to the
key "alpha" in the hash a.

Blessings,
TwP

  Réponse avec citation
Vieux 16/09/2007, 19h51   #4
Tim Hunter
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: bug with assignment?

Mukund wrote:
> The snippet illustrates my question.
>
> a={}
> b={}
> a["alpha"]=b["alpha"]=[]
> a["alpha"]<<23
> b["alpha"]<<100
> puts a["alpha"]
> puts b["alpha"]
>
> Here a["alpha"] and b["alpha"] both contain an array of [23,100].
> Why??
>
> If I remove the multiple assignment,
> a={}
> b={}
> a["alpha"]=[]
> b["alpha"]=[]
> a["alpha"]<<23
> b["alpha"]<<100
> puts a["alpha"]
> puts b["alpha"]
>
>
> a["alpha"] contains 23 and b["alpha"] contains 100 as I expected.
>
>

Because, in the first case, both a["alpha"] and b["alpha"] refer to the
same array. You simply have two different ways of referring to it.

In the second case, the code constructs two different arrays.

--
RMagick OS X Installer [http://rubyforge.org/projects/rmagick/]
RMagick Hints & Tips [http://rubyforge.org/forum/forum.php?forum_id=1618]
RMagick Installation FAQ [http://rmagick.rubyforge.org/install-faq.html]

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


Édité par : vBulletin®
Copyright ©2000 - 2009, 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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,22183 seconds with 12 queries