Patrick Gundlach wrote:
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>
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"
···
On Jun 3, 2005, at 2:05 PM, Patrick Gundlach wrote:
the last line gives me an error
-:11: syntax error
a.foo=("bar","baz")
^
[...]
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
--- Patrick Gundlach <clr5.10.randomuser@spamgourmet.com>
wrote:
[...]
> 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/
[...]
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