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

Brian Genisio briangenisio at gmail.com
Wed Jul 21 12:34:06 EDT 2010


This is a cross-post from Stack Overflow, but I haven't heard a peep there,
so I figured I'd try here:

I am playing around with the interop between C# and IronRuby.  I have
noticed that if I define a property in Ruby using `attr_accessor`, it is
presented to C# as a property.  If, on the other hand, I create the exact
same code manually, it comes back as a method.

For example, take this code:

    var engine = IronRuby.Ruby.CreateEngine();
    string script = @"
      class Test
        attr_accessor :automatic

        def manual
          @manual
        end

        def manual=(val)
          @manual = val
        end

        def initialize
          @automatic = ""testing""
          @manual = ""testing""
        end
      end

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

    var automatic = testObject.automatic;
    var manual = testObject.manual;

When you look at the C# `automatic` variable, the value is a string of
"testing".  If you look at the C# `manual` variable, it is type
IronRuby.Builtins.RubyMethod.

Ultimately, I want to create my own properties in Ruby that can be used in
C#, but I can't seem to make them be visible as properties like
`attr_accessor` does.

I THINK, that there is some magic going on in the Module code of the Ruby
source code (ModuleOps.cs:DefineAccessor).  Is there any way to do this in
Ruby code directly?

Thanks,
Brian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20100721/5f3a5ecc/attachment.html>


More information about the Ironruby-core mailing list