Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3321856
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T23:06:48+00:00 2026-05-17T23:06:48+00:00

I wrote a script that contains a few method definitions, no classes and some

  • 0

I wrote a script that contains a few method definitions, no classes and some public code. Some of these methods execute some pretty time-consuming shell programs. However, these shell programs only need to be executed the first time the method is invoked.

Now in C, I would declare a static variable in each method to make sure those programs are only executed once. How could I do that in Ruby?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T23:06:49+00:00Added an answer on May 17, 2026 at 11:06 pm

    There is an idiom in ruby: x ||= y.

    def something
      @something ||= calculate_something
    end
    
    private
    
    def calculate_something
      # some long process
    end
    

    But there is a problem with this idiom if your ‘long running utility’ may return a false value (false or nil), since the ||= operator will still cause the right side to be evaluated.
    If you expect false values then use an additional variable, in a way similar to the proposed by DigitalRoss:

    def something
      return @something if @something_calculated
      @something = calculate_something
      @something_calculated = true
      return @something
    end
    

    Don’t try to save a line of code by setting the @something_calculated variable first, an then running calculate_something. If your calculate function raises an exception your function will always return nil and will never call the calculate again.

    More generally, in Ruby you use instance variables. Note however, that they are visible in all the methods of given object – they are not local to the method.
    If you need a variable shared by all instances, define the method in the class object, and in every instance call self.class.something

    class User
      def self.something
        @something ||= calculate_something
      end
    
      def self.calculate_something
        # ....
      end
    
      def something
        self.class.something
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a crude korn shell script that someone else wrote. I don't know
I wrote a short bash script to complete a task that involves creating a
I have a Python script I recently wrote that I call using the command
I'd like to write a script that (under certain conditions) will execute gdb and
I'm trying to write a shell script that, when run, will set some environment
I want to write a script that will stop a scheduled task on a
I'm trying to write a script that informs the user when someone has logged
I'd like to write a script/batch that will bunch up my daily IIS logs
I'm trying to write a python script that packages our software. This script needs
I was sitting here wondering if it's possible to write a greasemonkey script that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.