method_missing and assignment

  • Thread starter Patrick Gundlach
  • Start date
P

Patrick Gundlach

Hi,

with ruby 1.8.2:
--------------------------------------------------
class A
def method_missing (a,*b)
# do something
end
end


a=A.new
a.foo("bar","baz")
a.foo=("bar","baz")
--------------------------------------------------

the last line gives me an error

-:11: syntax error
a.foo=("bar","baz")
^

Any way I can assign multiple values in a natural way with
method_missing? Or would you suggest to use an array that holds
multiple data?

Regards,

Patrick
 
J

Joel VanderWerf

Patrick said:
Hi,

with ruby 1.8.2:
--------------------------------------------------
class A
def method_missing (a,*b)
# do something
end
end


a=A.new
a.foo("bar","baz")
a.foo=("bar","baz")
--------------------------------------------------

the last line gives me an error

-:11: syntax error
a.foo=("bar","baz")
^

Any way I can assign multiple values in a natural way with
method_missing? Or would you suggest to use an array that holds
multiple data?

Regards,

Patrick

It's not particularly a property of method_missing--you can't even
define a method to do this:

irb(main):001:0> def foo=(a,b); end
=> nil
irb(main):002:0> self.foo=(1,2)
SyntaxError: compile error
(irb):2: syntax error
self.foo=(1,2)
^
from (irb):2
irb(main):003:0>
 
P

Patrick Gundlach

[...]
It's not particularly a property of method_missing--you can't even
define a method to do this:

irb(main):002:0> self.foo=(1,2)
SyntaxError: compile error
(irb):2: syntax error
self.foo=(1,2)
^
from (irb):2

I should have tested the obvoius, thanks for this out.

but something like method=(1,2) could be handy, for example for
assigning complex numbers.

Thanx,

Patrick
 
G

Gavin Kistner

the last line gives me an error

-:11: syntax error
a.foo=("bar","baz")
^


a.foo = ["bar","baz"];

You cannot have more than one value on the right side of a call to a
method whose name ends in =.

(But you can do:
a.foo, b.foo = "bar", "baz"
which is the same as:
a.foo = "bar"
b.foo = "baz"
 
E

Eric Mahurin

[...]
It's not particularly a property of method_missing--you can't even
define a method to do this:

irb(main):002:0> self.foo=(1,2)
SyntaxError: compile error
(irb):2: syntax error
self.foo=(1,2)
^
from (irb):2

I should have tested the obvoius, thanks for this out.

but something like method=(1,2) could be handy, for example
for
assigning complex numbers.

Thanx,

Patrick

The above isn't valid syntax because it is being parsed as an
assignment (not a method call) and (1,2) is not a valid
expression (RHS).

If you need something more complex, you can just assign with an
array:

self.foo = [1,2]

There is an RCR open to attempt to be able to do this type of
thing:

self.foo(*args) = value

which gets interpreted as self."foo="(*args,value). This is
similar to the []= operator.




__________________________________
Discover Yahoo!
Use Yahoo! to plan a weekend, have fun online and more. Check it out!
http://discover.yahoo.com/
 
P

Patrick Gundlach

[...]
The above isn't valid syntax because it is being parsed as an
assignment (not a method call) and (1,2) is not a valid
expression (RHS).

OK, I see.

If you need something more complex, you can just assign with an
array:

self.foo = [1,2]

This is the solution where I first thought of as a workaround. But it
looks ok to me now.


Thanks,

Pat
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top