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

Curt Hagenlocher curth at microsoft.com
Thu Jul 22 09:17:43 EDT 2010

I believe this works as designed.

The problem is that Ruby doesn't otherwise distinguish syntactically between a property and a method with no parameters. Imagine that you're in tooling such as Visual Studio. By default, the values of properties are automatically displayed in the debugger and in tool tips. But if I happen to have a no-args method named format_cdrive, I probably don't want that code to be run just to inspect its value. Effectively, attr_accessor, attr_reader and attr_writer are used by IronRuby as signals that indicate this operation is free of potentially-nasty side effects.

From: ironruby-core-bounces at rubyforge.org [mailto:ironruby-core-bounces at rubyforge.org] On Behalf Of Brian Genisio
Sent: Thursday, July 22, 2010 5:49 AM
To: ironruby-core at rubyforge.org
Subject: Re: [Ironruby-core] Why does attr_accessor create a property, but method is just a method?

So, I haven't heard anything about this yet on Stack Overflow, or this list.

Does anyone know if this is this a bug in IronRuby interop?

On Wed, Jul 21, 2010 at 12:34 PM, Brian Genisio <briangenisio at gmail.com<mailto:briangenisio at gmail.com>> wrote:
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

        def manual=(val)
          @manual = val

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

    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?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://rubyforge.org/pipermail/ironruby-core/attachments/20100722/b453524a/attachment.html>

More information about the Ironruby-core mailing list