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?

5
  • At what line exactly do you get this error? What's a result? May 6, 2012 at 16:06
  • Your comment syntax is invalid. And you can omit parentheses in if condition. May 6, 2012 at 16:07
  • To be specific, I don't believe the syntax is invalid -- however it's true that it's not needed. It's appropriate to use parentheses for clarification if you believe it's clearer. In this case it probably doesn't make a difference so leaving them off may be the best approach. I think it's up to you. May 6, 2012 at 16:10
  • It was my belief that is valid syntax and more a question of style and clarity. Do you disagree? Did I miss something? May 6, 2012 at 16:14
  • I was talking about comments. Single-line comments in ruby start with #, not //. May 6, 2012 at 16:15

2 Answers 2

2

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.

3
  • 1
    Doesn't "The Identifier specified does not exist" part confuse you? There's something more going on there than just simple hash access. May 6, 2012 at 16:10
  • 1
    It's better to use result.has_key? 'attribute_b' instead of !result['attribute_b'].nil?, because in some situations nil value can be valid.
    – Flexoid
    May 6, 2012 at 16:12
  • Possibly. But I think it's clear that he's probably got a null result so my first thought would be to determine why. You may be correct in that it's something outside what we're seeing here. May 6, 2012 at 16:12
0

First of all, your YAML structure looks bad (is it YAML?). You cannot have a hash with more than one element with the key item, because the key must be unique. You should use an array instead.

I suggest you structure you YAML along the lines of this here:

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

Then you can do

require 'yaml'
result = YAML.load(File.open 'foo.yml')
result['items'][0]['attribute_b']
=> "bacon"
3
  • Sorry, I'm doing thing.to_yaml in the real code. I just fudged how it's structured to simplify the question.
    – Tom
    May 6, 2012 at 16:15
  • got your question wrong… but result && result.has_key? is the way to go. May 6, 2012 at 16:17
  • ah I think someone else edited my question to make it clearer for everyone, thanks tho!
    – Tom
    May 6, 2012 at 16:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.