[Ironruby-core] Why does attr_accessor create a property, but method is just a method?

Tomas Matousek Tomas.Matousek at microsoft.com
Fri Jul 23 14:01:26 EDT 2010

Unless CRuby implements properties I don't like introducing property "names" that are different from foo, foo= pattern. For now that's just not the Ruby way. 


-----Original Message-----
From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Jörg W Mittag
Sent: Friday, July 23, 2010 9:16 AM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] Why does attr_accessor create a property, but method is just a method?

Brian Genisio wrote:
> It is a good idea, but I have tried this already, and it still fails.

So did I :-)

> By
> re-defining the method after attr_accessor, you blow away whatever 
> magic happens that causes it to be a property at interop time.
> Unless... you are suggesting that this is how it SHOULD work.

Yes, that's what I meant.

> I think either approach makes sense.  A special interop class method 
> is nice because it is explicit.  Having method redefinition do the job 
> is nice because it doesn't require a special class method :)
> What does everyone else think?

Actually, I believe both make sense. Having method redefinition not work simply violates at least *my* Principle of Least Astonishment, so it needs to be fixed anyway. But it alone is not enough: sometimes you might want to have different names for your .NET property and your Ruby methods. A good example might be a readonly boolean property:

    prop_getter :isValid => :valid?
    def valid?

Actually, now that I think about it, what about just overloading `attr_accessor` and friends? In plain Ruby, 

    attr_reader :isValid => :valid?

is illegal anyway. Together with working method redefinition, I believe this would cover all use cases, no?

[Note: the risk is that at some time in the future, Ruby might allow this notation to have the getter/setter names decoupled from the instance variable names. Actually, now that I think about it, I might suggest that right now :-) It always bugged me that you cannot simply define boolean accessors because that would try to create a @valid?
instance variable, which is illegal.]


Ironruby-core mailing list
Ironruby-core at rubyforge.org

More information about the Ironruby-core mailing list