Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Re: Assignment method strangeness
David Vallner
12/21/2006 11:23:00 AM
Citát Ryan Williams <mr.cruft@gmail.com>:
> Assignment methods are weird, and I wish they acted more like regular
> methods. It seems like they could be, but the parser doesn't properly parse
> the usage. Let me show you what I mean.
>
This is the third thread on the subject in the past month, IIRC. On the first
one, The Matz Hath Spoken:
http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-t...
This was an intentional change between 1.6 and 1.8, I think. Assignment is
assignment is assignment, if you do wildly un-assignmentlike things with it,
you'll confuse people reading your code - and you can as well use a method
without syntactic sugar around it for that.
> Assignment methods can't take more than one argument:
>
My best bet is that this is to avoid possible ambiguous parsing for parallel
assignment, which happily works for object attributes too:
irb(main):001:0> class Foo
irb(main):002:1> attr_accessor :bar, :baz
irb(main):003:1> end
=> nil
irb(main):004:0> foo = Foo.new
=> #<Foo:0x2f1924c>
irb(main):005:0> foo.bar, foo.baz = 'BAR', 'BAZ'
=> ["BAR", "BAZ"]
irb(main):006:0> foo.bar
=> "BAR"
irb(main):007:0> foo.baz
=> "BAZ"
> class Test
> def v=(&block)
> block.call
> end
> end
>
> test = Test.new
> test.v ={ puts "block" } #=> Exception: wrong number of arguments (1 for
> 0)
>
If I ever saw code where you go
> Is there any way around this?
>
At the risk of being painfully obvious, don't use an assignment? The assignment
method syntactic sugar is there to make setters, that is, methods that *set a
value to an attribute* look prettier and more importantly, behave consistently
within the language and with other common programming languages.
David Vallner
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Re: Assignment method strangeness
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password