That happens because the keys are symbols, and not strings starting
with ":". Try either using strings instead of symbols as hash keys
(i.e., 'finance_whitepaper' rather than :finance_whitepaper - note que
quotes) or using k.to_sym inside the loop.
···
On 6/13/07, kwest <kwestin@gmail.com> wrote:
Hello,
I am new to Ruby and have run into a small snag. I have a hash like
so:
where k is a variable in a loop with the key from the has minus the
":" in front
But if I try to add the ":" in front
<%key = ':' + k%>
And then try to reference the element
<%= @documentnames[key] %>
I get a null value....
The ":whatever" is a Ruby-Symbol, and you're trying to kinda "fake" it with a String. However, the answer is really easy, just use: @documentnames[k.to_sym]
> where k is a variable in a loop with the key from the has minus the
> ":" in front
> But if I try to add the ":" in front
> <%key = ':' + k%>
> And then try to reference the element
> <%= @documentnames[key] %>
> I get a null value....
The ":whatever" is a Ruby-Symbol, and you're trying to kinda "fake" it
with a String. However, the answer is really easy, just use: @documentnames[k.to_sym]