Re: How to define the assignment operator in Ruby ?



On Feb 21, 4:55 am, Stephane Wirtel <stephane.wir...@xxxxxxxxxxxxx>
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
.



Relevant Pages

  • Re: Refer to the first row of the table
    ... Syntax error in query expression 'afid>SELECT MinFROM ... Dim StrSQL As String ... FROM TblClients1 As T)" ... don't name your function "str". ...
    (microsoft.public.access.modulesdaovba)
  • [Full-Disclosure] (unsigned)-1 and large VM
    ... char *dup; ... str is large - 4GB-1. ... strcpy definitely screws some of the heap unless it segfaults. ... One should be able to supply in VM string of size -1 which is ...
    (Full-Disclosure)
  • Re: datatable.compute
    ... I am getting a Syntax error in the compute method of a datatable. ... SUM the qty field based on a ... filter expression and the expression contains 3 fields. ... TotalPickQty = Convert.ToInt32", str)) ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Refer to the first row of the table
    ... clever, gives me a syntax error and i must have written something ... You've left off one of the parentheses I had in my original SQL ... don't name your function "str". ...
    (microsoft.public.access.modulesdaovba)
  • Re: YANI solutions to perplexing things in the early game.
    ... NetHack does modify melee to-hit based on Str (since it's a clone of AD&D1 ... It's easier to smash your way through a enemy's ...
    (rec.games.roguelike.nethack)