[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 11:49:08 EDT 2010


Yes, this is one approached I've considered.

What about this case?

        class Test
          attr_accessor :a, :m
        end

        class Test2 < Test
          def m; @m end
          def m=(val) @m = val end

          def initialize; @a, @m = 'automatic', 'manual' end
        end

And this one?

        class Test
          attr_accessor :a, :m
          include M
        end

        class Test2
          include M
        end

        module M
          def m; @m end				# should this be a property accessor or not?
          def m=(val) @m = val end

          def initialize; @a, @m = 'automatic', 'manual' end
        end

I guess the rule could be that only redefinitions in the same class/module transfer the property of being a property accessor.

Another option might be to use attr_reader/attr_accessor. If no parameters are passed the next method becomes a property accessor. In MRI passing no parameters is possible and has no effect.

        class Test
          attr_reader
          def m; @m end

          attr_writer   # this is not really needed as any method whose name ends = could automatically be considered a property/indexer setter
          def m=(val) @m = val end

          def initialize; @a, @m = 'automatic', 'manual' end
        end

Tomas

-----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 8:17 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:
> Thinking on this further, if there were some sort of interop-specific 
> class method... like attr_clr_property :foo, that caused any methods 
> named foo and
> foo=(var) to be visible to .Net as properties, that would be sufficient.
> 
> Any thoughts on this?

Alternative suggestion: make method redefinition work in this case, like this:

    var engine = IronRuby.Ruby.CreateEngine();
    var script = @"
        class Test
          attr_accessor :a, :m

          def m; @m end
          def m=(val) @m = val end

          def initialize; @a, @m = 'automatic', 'manual' end
        end

        Test.new
    ";
    var testObject = engine.Execute(script);

    var automatic = testObject.a;
    var manual = testObject.m;

    Console.WriteLine(automatic);
    Console.WriteLine(manual);    // should print "manual"
    Console.ReadLine();

jwm

_______________________________________________
Ironruby-core mailing list
Ironruby-core at rubyforge.org
http://rubyforge.org/mailman/listinfo/ironruby-core



More information about the Ironruby-core mailing list