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 7498089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:19:52+00:00 2026-05-29T19:19:52+00:00

I’m trying to keep a hash local to one function that remembers its state

  • 0

I’m trying to keep a hash local to one function that remembers its state between calls to the function. But I don’t know how to declare it without a closure (as some users suggested in a similar thread).

I know C++ more thoroughly than ruby, and in C++, I would have ordinarily used a static local variable, like in the first example here: http://msdn.microsoft.com/en-us/library/s1sb61xd.aspx

I managed to hack something together in ruby using the defined? function:

def func x
  if not defined? @hash
    @hash = Hash.new
  end

  if @hash[x]
    puts 'spaghetti'
  else
    @hash[x] = true
    puts x.to_s
  end
end

func 1
func 1

This prints, the following, which is kind of what I want. The only problem is that @hash can be accessed outside of that function.

1
spaghetti

Is there any “cleaner”, more preferred way to declare a variable with this behavior (without a factory)? I was going to create two or three variables like @hash, so I was looking for a better way to express this concisely.

  • 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-29T19:19:54+00:00Added an answer on May 29, 2026 at 7:19 pm

    What you’re doing is pretty common in Ruby, but also so common you don’t need to make a big fuss about it. All @-type instance variables are local to that instance only. Keep in mind “instance” generally refers to an instance of a class, but it can refer to the instance of the class as well.

    You can use @@ to refer to a class instance variable from the context of an instance, but this tends to get messy in practice.

    What you want to do is one of the following.

    A variable that persists between method calls, but only within the context of a single object instance:

    def func(x)
      # Instance variables are always "defined" in the sense that
      # they evaluate as nil by default. You won't get an error
      # for referencing one without declaring it first like you do
      # with regular variables.
      @hash ||= { }
    
      if @hash[x]
        puts 'spaghetti'
      else
        @hash[x] = true
        puts x.to_s
      end
    end
    

    A variable that persists between method calls, but only within the context of all object instances:

    def func(x)
      # Instance variables are always "defined" in the sense that
      # they evaluate as nil by default. You won't get an error
      # for referencing one without declaring it first like you do
      # with regular variables.
      @@hash ||= { }
    
      if @@hash[x]
        puts 'spaghetti'
      else
        @@hash[x] = true
        puts x.to_s
      end
    end
    

    This is usually made cleaner by wrapping the @@hash into a class method. This has the secondary effect of making testing easier:

    def self.func_hash
      @func_hash ||= { }
    end
    
    def func(x)
      if self.class.func_hash[x]
        puts 'spaghetti'
      else
        self.class.func_hash[x] = true
        puts x.to_s
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.