# Virtual instance-variable accessors

**URL:** <https://rubytalk.org/t/virtual-instance-variable-accessors/12030>\
**Category:** ruby-talk\
**Created:** [10 June 2004 05:28 UTC](https://rubytalk.org/t/virtual-instance-variable-accessors/12030 "2004-06-10T05:28:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Austin\_McDonald](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@Austin\_McDonald](https://rubytalk.org/u/Austin_McDonald)\
**Post date:** [10 June 2004 05:28 UTC](https://rubytalk.org/t/virtual-instance-variable-accessors/12030/1 "2004-06-10T05:28:38Z")

</div>

When I access an private variable, using @var inside a method, is there any way to capture that using a C extension? Or am I forced to use rb\_iv\_set/get to keep the ruby instance variable up to date? I would like for @var to call a C function; can this be done?

I've looked at CShadow, and it seems like it requires you to use self.var, which I've already figured out how to do.

Austin McDonald

---

<div class="post-metadata">

**Author:** ![Joel\_VanderWerf1](https://avatars.discourse-cdn.com/v4/letter/j/94ad74/32.png) [@Joel\_VanderWerf1](https://rubytalk.org/u/Joel_VanderWerf1)\
**Post date:** [11 June 2004 19:03 UTC](https://rubytalk.org/t/virtual-instance-variable-accessors/12030/2 "2004-06-11T19:03:15Z")

</div>

Austin McDonald wrote:

> When I access an private variable, using @var inside a method, is there any way to capture that using a C extension? Or am I forced to use rb\_iv\_set/get to keep the ruby instance variable up to date? I would like for @var to call a C function; can this be done?

AFAIK, there's no way to do this without hacking ruby.

> I've looked at CShadow, and it seems like it requires you to use self.var, which I've already figured out how to do.

Yeah. If I knew a way to "hook" ivar access, CShadow would have used it. But ruby probably is more efficient without the hooks since it would always have to check for them, and allocate space for them.

One alternative is to declare the accessors protected.

---

<div class="post-metadata">

**Author:** ![Austin\_McDonald](https://avatars.discourse-cdn.com/v4/letter/a/3bc359/32.png) [@Austin\_McDonald](https://rubytalk.org/u/Austin_McDonald)\
**Post date:** [11 June 2004 20:14 UTC](https://rubytalk.org/t/virtual-instance-variable-accessors/12030/3 "2004-06-11T20:14:57Z")

</div>

I can see how it would be more efficient this way. Good work on CShadow, btw. Thanks!

Austin

Joel VanderWerf wrote:

> **···**
>
> > Austin McDonald wrote:
> > 
> > > When I access an private variable, using @var inside a method, is there any way to capture that using a C extension? Or am I forced to use rb\_iv\_set/get to keep the ruby instance variable up to date? I would like for @var to call a C function; can this be done?
> > 
> > AFAIK, there's no way to do this without hacking ruby.
> > 
> > > I've looked at CShadow, and it seems like it requires you to use self.var, which I've already figured out how to do.
> > 
> > Yeah. If I knew a way to "hook" ivar access, CShadow would have used it. But ruby probably is more efficient without the hooks since it would always have to check for them, and allocate space for them.
> > 
> > One alternative is to declare the accessors protected.
