0

Out of the following the last one does not work:

puts node.elasticsearch[:plugin][:jetty][:name]
puts node.elasticsearch[:plugin]['jetty'][:name]
puts node.elasticsearch[:plugin]["'#{entry}'"][:name]

What syntax do I need to follow in order to use a variable's value in the index when accessing a multi dimensional array?

UPDATE: I think entry is a String but I could be wrong so here's the statement that sets it up for you the educated helper to determine what's what:

Dir.entries("/var/plugins/").any? do |entry|
    puts node.elasticsearch[:plugin][:jetty][:name]
    puts node.elasticsearch[:plugin]['jetty'][:name]
    puts node.elasticsearch[:plugin]["'#{entry}'"][:name]
end

2 Answers 2

1

Your hash is indexed with symbols, so you should convert your string into a symbol, with entry.intern().

puts node.elasticsearch[:plugin][entry.intern][:name]

Edit: to_sym and intern are aliases of the String class. So entry has to be a string, which it seems to be according to your attempts.

Sign up to request clarification or add additional context in comments.

Comments

1

You should have a key String in your hash, but it look like you have symoblized keys in hour hash. In this case, convert your string to Symbol, before you use it as a key.

Try this code:

puts node.elasticsearch[:plugin][entry.to_sym][:name]

1 Comment

sorry i didn't scroll down and ended up using the answer from @philant before i ever saw yours ...

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.