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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:20:02+00:00 2026-05-26T12:20:02+00:00

I know there are other questions similar such as: Ruby: how to check if

  • 0

I know there are other questions similar such as:

  1. Ruby: how to check if variable exists within a hash definition
  2. Checking if a variable is defined?

But the answers aren’t fully satisfactory.

I have:

ruby-1.9.2-p290 :001 > a=Hash.new
 => {} 
ruby-1.9.2-p290 :002 > a['one']="hello"
 => "hello"
ruby-1.9.2-p290 :006 > defined?(a['one']['some']).nil?
 => false 
ruby-1.9.2-p290 :007 > a['one']['some'].nil?
 => true

It seems like:

if a['one']['some'].nil?
  a['one']['some']=Array.new
end 

would be sufficient. Is this correct? Would this be correct for any data type? Is defined? needed in this case?

thx

  • 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-26T12:20:03+00:00Added an answer on May 26, 2026 at 12:20 pm

    You seem to be confusing two concepts. One is if a variable is defined, and another is if a Hash key is defined. Since a hash is, at some point, a variable, then it must be defined.

    defined?(a)
    # => nil
    
    a = { }
    # => {}
    
    defined?(a)
    # => "local-variable"
    
    a.key?('one')
    # => false
    
    a['one'] = 'hello'
    # => 'hello'
    
    a.key?('one')
    # => true
    

    Something can be a key and nil at the same time, this is valid. There is no concept of defined or undefined for a Hash. It is all about if the key exists or not.

    The only reason to test with .nil? is to distinguish between the two possible non-true values: nil and false. If you will never be using false in that context, then calling .nil? is unnecessarily verbose. In other words, if (x.nil?) is equivalent to if (x) provided x will never be literally false.

    What you probably want to employ is the ||= pattern that will assign something if the existing value is nil or false:

    # Assign an array to this Hash key if nothing is stored there
    a['one']['hello'] ||= [ ]
    

    Update: Edited according to remarks by Bruce.

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

Sidebar

Related Questions

I know there are other similar questions, but I would like to know specifically
As far as i know there are many other questions similar to title, but
I know this may seem similar to other questions on here but just here
I know there are several similar questions, but I'm struggling to understand the error
I know there are similar questions on stackoverflow - and I looked through them
I know there's similar questions to this, but the approved answers don't seem to
I know there are very similar questions and answers posted. And I have used
I know there are lots of similar questions floating around, but none of the
Probably there are a lot of similar questions around, but since I know only
Does anyone know there have any other way that (by not using json_encode and

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.