Phrogz wrote:
Would you really type:
class Foo
attr_accessor( :bar )
end
Yes, I would and I'll tell you why. attr_accessor is a method call. Vincent Fourmond mentioned alias, which isn't, but how would I know that? For example:
class Foo
attr_accessor :foo, :bar
alias :baz, :foo
end
I would expect this to be syntactically correct. It isn't, though. alias is a keyword that seems to be syntactic sugar for alias_method(:baz,:foo)
If parentheses were mandatory the class would look like this:
class Foo
attr_accessor(:foo, :bar)
alias :baz :foo
end
I think that it is clearer in the above code, that there is a difference between attr_accessor and alias than
class Foo
attr_accessor :foo, :bar
alias :baz :foo
end
Then again, different strokes and all that.
f = Foo.new()
I'd buy that. Guess which language I'm coming from
f.bar=( 12 )
Ugh. Good point. That you can write f.bar = 12 instead of f.bar=(12) or, God forbid, f.set_bar(12) is something I really like about Ruby. However, there are more syntactic sugar at work here. If not, you shouldn't really be allowed to write f.bar = 12 and this trick only works for a small number of "special" methods (-,*,<<,etc) (I think). It doesn't seem to be all that big of a deal to make f.bar = something syntactic sugar for f.bar=(something).
puts( f.bar() )
I'll buy that too.
One of the greatest reasons for omitting parentheses is that the
syntactic sugar allows you to write code that *looks* like you're
assigning values to properties and reading those properties, when in
reality you're calling methods.
f.bar = 12
puts( f.bar )
I'm all for writing code that is aesthetically pleasing, and to be honest, I like that you can omit parentheses. What I don't like is the quirks it introduces such as the one in the original post. Another example is
a +b
which may or may not throw an exception depending on whether or not +@ is defined for b, but if the parser decides that a is a method call it is parsed as a(+b), which probably wasn't the idea. If the parser decides that a is a variable it parses it as a.+(b) which is probably correct. Explicit parentheses would again remove the ambiguity, I think. This example is in the FAQ and in at least one recent post in here, so I guess people are actually puzzled by this (with good reason, IMHO).
So it's really a question of cost vs. benefits. I think the cost is quite high. Then again, I'm new to Ruby.
Best Regards,
Henrik Schmidt