# Efficient access of instance variables

**URL:** <https://www.ruby-forum.com/t/efficient-access-of-instance-variables/158240>\
**Category:** Ruby\
**Created:** [January 21, 2009, 12:31am UTC](https://www.ruby-forum.com/t/efficient-access-of-instance-variables/158240 "2009-01-21T00:31:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [January 21, 2009, 12:31am UTC](https://www.ruby-forum.com/t/efficient-access-of-instance-variables/158240/1 "2009-01-21T00:31:43Z")

</div>

I’m having my first extended day working with classes - trying to  
convert some functioning methods in my current project to classes.

I’m looking for a way to call a class instance and get back a couple of  
its instance variables with an efficiency equivalent to this method  
call:

@log, @logging\_now = manage\_log( ‘open’ ) # the method returns both vars

So, I seem to have write all this -

manlog = ManageLog.new  
manlog.open  
log, logging\_now = manlog.log, manlog.logging\_now

Before I resign myself, I want to ask if I’m missing something here. Is  
there a way I can do any of this more tersely?

Thanks for any help,

Tom

–

```auto
Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

```

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [January 21, 2009, 1:51am UTC](https://www.ruby-forum.com/t/efficient-access-of-instance-variables/158240/2 "2009-01-21T01:51:19Z")

</div>

Tom C. wrote:

> 

Scratch this call for help. I getting it figure out. ~t.

–

```auto
Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

```

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [January 21, 2009, 5:25am UTC](https://www.ruby-forum.com/t/efficient-access-of-instance-variables/158240/3 "2009-01-21T05:25:36Z")

</div>

Hi Tom,

Am Mittwoch, 21. Jan 2009, 09:49:04 +0900 schrieb Tom C.:

> Tom C. wrote:
> 
> > manlog = ManageLog.new  
> > manlog.open  
> > log, logging\_now = manlog.log, manlog.logging\_now
> 
> Scratch this call for help. I getting it figure out. ~t.

Anyway I’m egged to advertise my private philosophy.

class ManageLog  
class \<\<self  
private :new  
def open  
o = new  
o.open  
yield o  
end  
end  
def logged  
yield $stdout, $stderr  
end  
end

ManageLog.open |m| do  
m.logged do |log,log\_now|  
…  
end  
end

Or maybe

class ManageLog  
def log\_my\_pipes stdout, stderr  
$stdout.reopen stdout  
$stderr.reopen stderr  
yield  
ensure  
$stdout.reopen STDOUT  
$stderr.reopen STDERR  
end  
end

This is, of course, untested and just a suggestion.

Bertram
