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 > How to define the assignment operator in Ruby ?
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
How to define the assignment operator in Ruby ?

Réponse
 
LinkBack Outils de la discussion
Vieux 21/02/2008, 12h55   #1
Stephane Wirtel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut How to define the assignment operator in Ruby ?

Hi all,

How to define the assignment operator for a class ?

irb(main):012:0> class Test
irb(main):013:1> def = other
irb(main):014:2> puts other.class
irb(main):015:2> end
irb(main):016:1> end
SyntaxError: compile error
(irb):13: syntax error, unexpected '='
def = other
^
(irb):16: syntax error, unexpected kEND, expecting $end from (irb):16

Is there a way to solve my issue ?

Where is my mistake ?

I don't believe that's possible to assign something to an instance

Best Regards,

Stephane
  Réponse avec citation
Vieux 21/02/2008, 14h10   #2
Ilan Berci
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to define the assignment operator in Ruby ?

Stephane Wirtel wrote:

>
> I don't believe that's possible to assign something to an instance
>
> Best Regards,
>
> Stephane


Stephanie,

In ruby, we don't assign to an instance as you mentioned, we assign to a
variable that can hold any type. We therefore can't override the
assignment operator.

BUT you can override an assignment operation.. such as

class Foo
def bar= (val)
@bar = val
p "I am assigning #{val} to @bar"
end

hth

ilan

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

  Réponse avec citation
Vieux 21/02/2008, 19h56   #3
Stéphane Wirtel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to define the assignment operator in Ruby ?

> Stephanie,
Stephane and not Stephanie :-) Stephanie is the firstname for a girl
>
> In ruby, we don't assign to an instance as you mentioned, we assign to a
> variable that can hold any type. We therefore can't override the
> assignment operator.
>
> BUT you can override an assignment operation.. such as
>
> class Foo
> def bar= (val)
> @bar = val
> p "I am assigning #{val} to @bar"
> end

Grrr, so I have reason about this operator, I can't override it.

Thanks
  Réponse avec citation
Vieux 22/02/2008, 00h04   #4
Doug
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to define the assignment operator in Ruby ?

On Feb 21, 4:55 am, Stephane Wirtel <stephane.wir...@descasoft.com>
wrote:
> Hi all,
>
> How to define the assignment operator for a class ?
>
> irb(main):012:0> class Test
> irb(main):013:1> def = other
> irb(main):014:2> puts other.class
> irb(main):015:2> end
> irb(main):016:1> end
> SyntaxError: compile error
> (irb):13: syntax error, unexpected '='
> def = other
> ^
> (irb):16: syntax error, unexpected kEND, expecting $end from (irb):16
>
> Is there a way to solve my issue ?
>
> Where is my mistake ?
>
> I don't believe that's possible to assign something to an instance
>
> Best Regards,
>
> Stephane


Stephane,

What exactly are you trying to do? If you are trying to implement a
method that copies an object, this is supported in the language via
the dup() or clone() methods:

str = "I am a string."
copy_of_str = str.clone
str.object_id should not equal copy_of_str.object_id

For your own objects, the default clone and dup methods perform
shallow copies of state (references are copied). You would have to
override clone or dup to do anything more complicated.

class A
attr_accessor :str
def initialize(str)
@str = str
end
end

a = A.new("test")
b = a.clone

a.object_id != b.object_id

but

a.str.object_id == b.str.object_id

Is this what you were getting at?

-Doug
  Réponse avec citation
Vieux 22/02/2008, 00h53   #5
Gary Wright
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to define the assignment operator in Ruby ?


On Feb 21, 2008, at 9:10 AM, Ilan Berci wrote:
> In ruby, we don't assign to an instance as you mentioned, we assign
> to a
> variable that can hold any type. We therefore can't override the
> assignment operator.
>
> BUT you can override an assignment operation.. such as


Setter methods may look like assignment, but semantically they are
method calls. I think it just confuses matters to call setter method
invocation an 'assignment operation'.

The desire to override assignment or define an assignment operator is
generally indicative of some misunderstanding regarding Ruby's object
model.

Gary Wright

  Réponse avec citation
Vieux 22/02/2008, 10h44   #6
Stephane Wirtel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to define the assignment operator in Ruby ?

> The desire to override assignment or define an assignment operator is
> generally indicative of some misunderstanding regarding Ruby's object
> model.

It's not a misunderstanding, I wanted to be sure that it was not
possible to do it.

I had a doubt because I discussed with a friend about python and this
operator, and he tells me that is possible to override it.

Today, after googling, my friend tells me that's not possible to
override this operator ;-)
>
> Gary Wright


Thanks

Stephane
  Réponse avec citation
Vieux 22/02/2008, 10h49   #7
Ron Fox
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: How to define the assignment operator in Ruby ?

Exactly.. since assignment in Ruby is really assigning a >reference<
to an object to a variable rather than copying an existing object
to a new one. For example:

a = "some String"
b = a
b << " more text"

print "#{a}"

Will print "some String more text" not "some String" because a and b
refer to the same objects rather than distinct copies of an object
that contains "some String". This is why the dup/clone methods exist.

Ron

Gary Wright wrote:
> On Feb 21, 2008, at 9:10 AM, Ilan Berci wrote:
>> In ruby, we don't assign to an instance as you mentioned, we assign
>> to a
>> variable that can hold any type. We therefore can't override the
>> assignment operator.
>>
>> BUT you can override an assignment operation.. such as

>
> Setter methods may look like assignment, but semantically they are
> method calls. I think it just confuses matters to call setter method
> invocation an 'assignment operation'.
>
> The desire to override assignment or define an assignment operator is
> generally indicative of some misunderstanding regarding Ruby's object
> model.
>
> Gary Wright
>

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


É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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,14315 seconds with 15 queries