Forum: Ruby efficient access of instance variables

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-01-21 00:31
(Received via mailing list)
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

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-01-21 01:51
(Received via mailing list)
Tom Cloyd wrote:
>
>
Scratch this call for help. I getting it figure out. ~t.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< tc@tomcloyd.com >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
C06869c119472a139eb163b72040b0db?d=identicon&s=25 Bertram Scharpf (Guest)
on 2009-01-21 05:25
(Received via mailing list)
Hi Tom,

Am Mittwoch, 21. Jan 2009, 09:49:04 +0900 schrieb Tom Cloyd:
> Tom Cloyd 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
This topic is locked and can not be replied to.