2

I have a config YAML file with the following array:

EMAILS: ['[email protected]', '[email protected]', '[email protected]']

And then I want to search inside this YAML array for a particular string, say "[email protected]".

I use the following ruby code:

if CONFIG[:EMAILS].include?("[email protected]")
  return true
else
  return false
end

However, this always returns false.

I can PUTS the CONFIG[:EMAIL] array fine.

Can anyone suggest what I'm doing wrong.

2
  • Your ruby code assumes that the YAML has already been loaded into a hash. How did that happen? Commented Nov 8, 2012 at 16:07
  • Can you post the output of CONFIG[:EMAILS] please? and how you parse the yaml file? Commented Nov 8, 2012 at 16:11

1 Answer 1

2

You're using a symbol, while the YAML hash uses strings as keys. This should work:

if CONFIG['EMAILS'].include?("[email protected]")
  return true
else
  return false
end

Which, by the way, is equivalent to simply

return CONFIG['EMAILS'].include?("[email protected]")
Sign up to request clarification or add additional context in comments.

4 Comments

The return part is completely redundant if you have this as your last line of a method. Presumably this line would be given the previous behavior was to exit regardless of the condition.
In both cases it would exit the function, so if there was code after that it wouldn't have been executed.
Sure. It's still a big "if" and therefore not equivalent in all circumstances.
I was pretty clear that if that was the last line, then the return is not required. This is basic Ruby behavior. It is not controversial. Under all circumstances where it's the last line, it is equivalent.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.