method_missing and assignment

Discussion in 'Ruby' started by Patrick Gundlach, Jun 3, 2005.

  1. 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
     
    Patrick Gundlach, Jun 3, 2005
    #1
    1. Advertising

  2. 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>
     
    Joel VanderWerf, Jun 3, 2005
    #2
    1. Advertising

  3. multiple assignments (was: method_missing and assignment)

    [...]

    > 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, Jun 3, 2005
    #3
  4. 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")
    > ^



    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"
     
    Gavin Kistner, Jun 3, 2005
    #4
  5. Patrick Gundlach

    Eric Mahurin Guest

    Re: multiple assignments (was: method_missing and assignment)

    --- Patrick Gundlach <>
    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/
     
    Eric Mahurin, Jun 3, 2005
    #5
  6. Re: multiple assignments

    [...]

    > 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
     
    Patrick Gundlach, Jun 3, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Neil Mc Laughlin

    method_missing and send

    Neil Mc Laughlin, Jun 14, 2004, in forum: Ruby
    Replies:
    7
    Views:
    169
    Paul Brannan
    Jun 16, 2004
  2. zuzu
    Replies:
    0
    Views:
    106
  3. Kirk Haines

    DRb and method_missing?

    Kirk Haines, Aug 3, 2005, in forum: Ruby
    Replies:
    1
    Views:
    118
    Eric Hodel
    Aug 3, 2005
  4. John Lam
    Replies:
    2
    Views:
    105
    John Lam
    Sep 24, 2005
  5. John Lam
    Replies:
    4
    Views:
    162
    Dave Burt
    Nov 10, 2005
Loading...

Share This Page