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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:58:29+00:00 2026-06-06T14:58:29+00:00

Obviously ||= won’t work def x? @x_query ||= expensive_way_to_calculate_x end because if it turns

  • 0

Obviously ||= won’t work

def x?
  @x_query ||= expensive_way_to_calculate_x
end

because if it turns out to be false or nil, then expensive_way_to_calculate_x will get run over and over.

Currently the best way I know is to put the value into an Array:

def x?
  return @x_query.first if @x_query.is_a?(Array)
  @x_query = [expensive_way_to_calculate_x]
  @x_query.first
end

Is there a more conventional or efficient way of doing this?

UPDATE I realized that I wanted to memoize nil in addition to false – this goes all the way back to https://rails.lighthouseapp.com/projects/8994/tickets/1830-railscachefetch-does-not-work-with-false-boolean-as-cached-value – my apologies to Andrew Marshall who gave an otherwise completely correct answer.

  • 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-06-06T14:58:29+00:00Added an answer on June 6, 2026 at 2:58 pm

    Explicitly check if the value of @x_query is nil instead:

    def x?
      @x_query = expensive_way_to_calculate_x if @x_query.nil?
      @x_query
    end
    

    Note that if this wasn’t an instance variable, you would have to check if it was defined also/instead, since all instance variables default to nil.

    Given your update that @x_query‘s memoized value can be nil, you can use defined? instead to get around the fact that all instance variables default to nil:

    def x?
      defined?(@x_query) or @x_query = expensive_way_to_calculate_x
      @x_query
    end
    

    Note that doing something like a = 42 unless defined?(a) won’t work as expected since once the parser hits a =, a is defined before it reaches the conditional. However, this isn’t true with instance variables since they default to nil the parser doesn’t define them when it hits =. Regardless, I think it’s a good idiom to use or or unless‘s long block form instead of a one-line unless with defined? to keep it consistent.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Obviously, the typical WebForms approach won't work. How does one track a user in
This code obviously won't work but I'm looking for a syntax change to make
EDIT: Solved this myself - obviously won't work as sorting the dataTable doesn't sort
The code below obviously won't work. I am not familiar with how to return
I've tried everything... it won't hide. Obviously tried Mouse.hide() tried different player versions (10.2,
I'd like the following code to work: double num = 31415; /* num will
I'm creating software for an Linux + AVR Arduino project. Obviously the whole work
Assume the following code, without any ref keyword, that obviously won't replace the variable
Obviously best-case is O(n), but apparently the worst-case is O(n 2 ), which I
Obviously MVC promotes separation of concerns. One thing we are struggling with is proper

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.