# Accessor methods and property syntax in .NET

**URL:** <https://www.ruby-forum.com/t/accessor-methods-and-property-syntax-in-net/194569>\
**Category:** IronRuby\
**Created:** [September 7, 2010, 10:37am UTC](https://www.ruby-forum.com/t/accessor-methods-and-property-syntax-in-net/194569 "2010-09-07T10:37:08Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Davy\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ba0ec/32.png) [@Davy\_B](https://www.ruby-forum.com/u/Davy_B)\
**Post date:** [September 7, 2010, 10:37am UTC](https://www.ruby-forum.com/t/accessor-methods-and-property-syntax-in-net/194569/1 "2010-09-07T10:37:08Z")

</div>

Hi all,

IronRuby has a nice trick where attributes defined by attr\_reader,  
attr\_accessor and attr\_writer are usable as properties from C#… i was  
just  
wondering: is it possible to use the same trick in our own ruby code?

or can anyone just point me in the right direction as to where in the  
IronRuby source code i could find where this is being done?

I have accessor methods that really need to do something specific and  
ideally, i’d like them be usable as properties in .NET since it leads to  
a  
much nicer syntax in C#

brs,  
Davy

---

<div class="post-metadata">

**Author:** ![Davy\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ba0ec/32.png) [@Davy\_B](https://www.ruby-forum.com/u/Davy_B)\
**Post date:** [September 7, 2010, 11:06am UTC](https://www.ruby-forum.com/t/accessor-methods-and-property-syntax-in-net/194569/2 "2010-09-07T11:06:25Z")

</div>

You can just define them as attr\_accessors and then implement the  
methods

class Foo

attr\_accessor :bar, :baz

def bar=(val)  
# do more stuff here for example  
@bar = val  
end

def bar  
@bar  
end

end

---

<div class="post-metadata">

**Author:** ![Davy\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ba0ec/32.png) [@Davy\_B](https://www.ruby-forum.com/u/Davy_B)\
**Post date:** [September 7, 2010, 11:14am UTC](https://www.ruby-forum.com/t/accessor-methods-and-property-syntax-in-net/194569/3 "2010-09-07T11:14:01Z")

</div>

if i do that, calling bar as a property like this:

var value = myObject.bar;

leads to ‘value’ being an instance of IronRuby.BuiltIns.RubyMethod, and  
not  
the value that is in the @bar instance field

which

i’m using 1.1 btw, not sure how this would behave on 1.0

from looking at the IronRuby source code, it looks like i need to find a  
way  
to change the RubyMemberInfo for a given accessor method to  
RubyAttributeReaderInfo or RubyAttributeWriterInfo

---

<div class="post-metadata">

**Author:** ![Davy\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ba0ec/32.png) [@Davy\_B](https://www.ruby-forum.com/u/Davy_B)\
**Post date:** [September 7, 2010, 11:56am UTC](https://www.ruby-forum.com/t/accessor-methods-and-property-syntax-in-net/194569/4 "2010-09-07T11:56:11Z")

</div>

your solution does however work for the assignment accessor… in fact,  
you  
can do something like this:

class Foo  
attr\_reader :bar

def bar=(val)  
# do more stuff here for example  
@bar = val  
end

end

and then you can do the following in C#:

var value = myObject.bar;  
myObject.bar = someOtherValue;

for my specific scenario, i think i can actually get by with just  
overriding  
the assignment accessor but the thing i’m wondering is: is the current  
behavior actually a bug? and will this behavior be preserved in future  
releases?

---

<div class="post-metadata">

**Author:** ![Davy\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ba0ec/32.png) [@Davy\_B](https://www.ruby-forum.com/u/Davy_B)\
**Post date:** [September 7, 2010, 2:47pm UTC](https://www.ruby-forum.com/t/accessor-methods-and-property-syntax-in-net/194569/5 "2010-09-07T14:47:44Z")

</div>

Actually, we discussed this about a month ago:  
[http://rubyforge.org/pipermail/ironruby-core/2010-July/007154.html](http://rubyforge.org/pipermail/ironruby-core/2010-July/007154.html)

[http://rubyforge.org/pipermail/ironruby-core/2010-July/007154.html](http://rubyforge.org/pipermail/ironruby-core/2010-July/007154.html)The  
outcome was that a feature request was added to allow for C# properties  
to  
be defined using getter/setter in ruby. I was going to look at  
implementing  
it… got into a bit, and had a baby… Haven’t looked at it since.

B

---

<div class="post-metadata">

**Author:** ![Davy\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ba0ec/32.png) [@Davy\_B](https://www.ruby-forum.com/u/Davy_B)\
**Post date:** [September 7, 2010, 2:57pm UTC](https://www.ruby-forum.com/t/accessor-methods-and-property-syntax-in-net/194569/6 "2010-09-07T14:57:06Z")

</div>

Hey Brian,

i saw that thread, but didn’t really see a definitive answer as to how  
to  
work around it until it’s implemented (though i might’ve missed it),  
which  
is why i thought i’d raise the question again 🙂

---

<div class="post-metadata">

**Author:** ![Davy\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ba0ec/32.png) [@Davy\_B](https://www.ruby-forum.com/u/Davy_B)\
**Post date:** [September 7, 2010, 2:57pm UTC](https://www.ruby-forum.com/t/accessor-methods-and-property-syntax-in-net/194569/7 "2010-09-07T14:57:06Z")

</div>

Beware!  
Contributing to the IronRuby source code might result in having  
babies!!!

🙂  
Congratulations Brian!

---

<div class="post-metadata">

**Author:** ![Davy\_B](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/7ba0ec/32.png) [@Davy\_B](https://www.ruby-forum.com/u/Davy_B)\
**Post date:** [September 9, 2010, 11:05am UTC](https://www.ruby-forum.com/t/accessor-methods-and-property-syntax-in-net/194569/8 "2010-09-09T11:05:36Z")

</div>

I love how he just tossed that off like it happens to everybody.

“Yeah, I know, I was committing this source patch, but all of a sudden  
there  
was this crying squalling thing in my lap that needed a diaper changed.  
WTF  
is up with that? . I know! Seriously! I thought good code wasn’t  
supposed to  
have side effects?!? I kept looking to ROLLBACK, but sonuvagun, wouldn’t  
you  
know it…”

Congrats, Brian.

Ted Neward

Java, .NET, XML Services

Consulting, Teaching, Speaking, Writing

[http://www.tedneward.com](http://www.tedneward.com)

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of Shay F.  
Sent: Tuesday, September 07, 2010 5:57 AM  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Ironruby-core] accessor methods and property syntax in  
.NET

Beware!

Contributing to the IronRuby source code might result in having  
babies!!!

🙂

Congratulations Brian!

On Tue, Sep 7, 2010 at 3:47 PM, Brian G. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

Actually, we discussed this about a month ago:  
[http://rubyforge.org/pipermail/ironruby-core/2010-July/007154.html](http://rubyforge.org/pipermail/ironruby-core/2010-July/007154.html)

The outcome was that a feature request was added to allow for C#  
properties  
to be defined using getter/setter in ruby. I was going to look at  
implementing it… got into a bit, and had a baby… Haven’t looked at  
it  
since.

B

On Tue, Sep 7, 2010 at 5:55 AM, Davy B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

your solution does however work for the assignment accessor… in fact,  
you  
can do something like this:

class Foo  
attr\_reader :bar

def bar=(val)

```
# do more stuff here for example

@bar = val

```

end

end

and then you can do the following in C#:

var value = myObject.bar;  
myObject.bar = someOtherValue;

for my specific scenario, i think i can actually get by with just  
overriding  
the assignment accessor but the thing i’m wondering is: is the current  
behavior actually a bug? and will this behavior be preserved in future  
releases?

On Tue, Sep 7, 2010 at 11:13 AM, Davy B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

if i do that, calling bar as a property like this:

var value = myObject.bar;

leads to ‘value’ being an instance of IronRuby.BuiltIns.RubyMethod, and  
not  
the value that is in the @bar instance field

which

i’m using 1.1 btw, not sure how this would behave on 1.0

from looking at the IronRuby source code, it looks like i need to find a  
way  
to change the RubyMemberInfo for a given accessor method to  
RubyAttributeReaderInfo or RubyAttributeWriterInfo

On Tue, Sep 7, 2010 at 11:01 AM, Ivan Porto Carerro  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

You can just define them as attr\_accessors and then implement the  
methods

class Foo

attr\_accessor :bar, :baz

def bar=(val)

```
# do more stuff here for example

@bar = val

```

end

def bar

```
@bar

```

end

end

On Tue, Sep 7, 2010 at 10:36 AM, Davy B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

Hi all,

IronRuby has a nice trick where attributes defined by attr\_reader,  
attr\_accessor and attr\_writer are usable as properties from C#… i was  
just  
wondering: is it possible to use the same trick in our own ruby code?

or can anyone just point me in the right direction as to where in the  
IronRuby source code i could find where this is being done?

I have accessor methods that really need to do something specific and  
ideally, i’d like them be usable as properties in .NET since it leads to  
a  
much nicer syntax in C#

brs,  
Davy

* * *

Ironruby-core mailing list  
removed\_email\_address@domain.invalid  
[http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)

* * *

Ironruby-core mailing list  
removed\_email\_address@domain.invalid  
[http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)

* * *

Ironruby-core mailing list  
removed\_email\_address@domain.invalid  
[http://rubyforge.org/mailman/listinfo/ironruby-core](http://rubyforge.org/mailman/listinfo/ironruby-core)
