"Trans" <transfire@gmail.com> schrieb im Newsbeitrag news:1106585454.733119.216360@z14g2000cwz.googlegroups.com...
. def a?
. @a ? true : @a
. end
Why not "def a?() @a end"?
Becasue that would be the exact same as 'attr :a'. What would be the
point? This guaruntees only three possible return values: true, false,
and nil.
So you want to protect the real value. Is that the reasoning behind this?
. def b!(x)
. @b.replace x
. end
Hm, I don't like that because it's too special case.
Well, these _are_ special cases. Don't see any reason not to like them
b/c of that. Just dont use them. Since the "functional space" is simply
going unused anyway (i.e. symbols can end in ? and ! without quoting),
and it coorepsonds to certain forms of method definitions, I gave them
the most reasonble base-line usage I could think of.
Still you introduce a method in class Module that has some special behavior which at least clutters namespace - even if unused. As Module is a very basic class it's behavior should be most general, too - my 0.02 EUR. (You can always define an add on that adds these methods.)
Also you might want to be able to assign and to replace...
I think you'd just replace first from the calling routine, and then
assign. Or perhaps I misunderstand?
I meant that with only the replace version of the method there is no option to assign to the member var. But with the getter you can already replace. Of course, a.c! "foo" is shorter than a.c.replace "foo" - but then again, it's a special case as it applies only to String (or at least mostly). 
Although I find "attr_accessor" quite lengthy, I don't
really see the benefit of your proposal. And attr_accessor
does indeed define reader and writer for several attributes
Ah, but there are many advantages, not just the fact that attr is
shorter. You can define both readers and writers in the same call.
I can do that with attr_accessor.
The
names of the methods defined are returned so you can use public,
private, protected (and user defined methods too!) in front of them.
This works only if the method returns a single symbol. Otherwise it's going to be ugly:
class Module
def a(*x) :single end
def b(*x) [:a,:b] end
end
class Foo
def single; end
def a;end
def b;end
private a :a, :b
private b :c, :d # doesn't work
private *b :c, :d # doesn't work
private *b( :c, :d) # ugly
end
They also route through a common #define_attribute method, so there is
central control, and it stores all defined attribute methods so you can
ask for a list of them. It also has casting, somthing I came up with a
few years ago. Eg.
. attr :a => :to_s
which defines
. def a
. @a.to_s
. end
Interesting.
I'm sorry that I don't share your enthusiasm - I simply don't miss this functionality.
Regards
robert