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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:13:07+00:00 2026-06-03T14:13:07+00:00

I have a hash which looks like this: items: item: attribute_a: cheese attribute_b: bacon

  • 0

I have a hash which looks like this:

items:
    item:
        attribute_a: cheese
        attribute_b: bacon
    item:
        attribute_a: salmon
    item:
        attribute_a: mushrooms
        attribute_b: steak

I would like to get the value of attribute_b, I’m using the following:

if (result['attribute_b'])
  // do something
end

However if attribute_b is missing, I get an error:

The Identifier specified does not exist, undefined method '[] for nil:NilClass'

What is the (best) correct way to check if attribute_b exists?

  • 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-03T14:13:09+00:00Added an answer on June 3, 2026 at 2:13 pm

    It looks as if you’re getting the error not upon accessing the attribute 'attribute_b', but because result is nil.

    The Identifier specified does not exist, undefined method [] for nil:NilClass`
    

    It’s saying you’re calling the method [] on a nil value. The only thing you’re calling ‘[]‘ on is result.

    The way you’re accessing 'attribute_b' is acceptable in general — I might be more specific and say:

    if (result && result.has_key? 'attribute_b')
     // do something
    end
    

    This will make sure that result exists as well as making sure the attribute is not null.

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

Sidebar

Related Questions

I have hash(Hash.from_xml) which looks like this (from inspect): { FileName=>hofplayers.xml, Version=>1.0, UserID=>3955847, FetchedDate=>2011-08-16
I have a ruby hash which looks like {10.1.1.6=>nick, 127.0.0.1=>nick1} But I can't manage
I have a string which looks like a hash: { :key_a => { :key_1a
I have @hash that looks like this: [1, {:clid=>1, :nvz=>4, :tip=>IP, :name=>Mark, :record=>some text}]
I have a nested hash table that looks like this: my %myhash = (
I have a hash being posted to my Rails app that looks like this:
I have a table which looks a bit like this: <table> <thead> <tr> <th></th>
I have an array of hashes. Each entry looks like this: - !map:Hashie::Mash name:
I have a hash in which I store the products a customer buys (
What I can think of is: Algo: Have a hash table which will store

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.