[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Ruby Hash Querying

kwest

6/13/2007 6:30:00 PM

Hello,
I am new to Ruby and have run into a small snag. I have a hash like
so:

@documentnames = {
:finance_whitepaper => 'whitepaper one',
:ecommerce_whitepaper => 'whitepaper two',
:integratedsvs_datasheet => 'whitepaper three'
}



So I am trying to then go like so:

@documentnames[k]

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....

4 Answers

Daniel Kempkens

6/13/2007 6:37:00 PM

0

kwest schrieb:
> Hello,
> I am new to Ruby and have run into a small snag. I have a hash like
> so:
>
> @documentnames = {
> :finance_whitepaper => 'whitepaper one',
> :ecommerce_whitepaper => 'whitepaper two',
> :integratedsvs_datasheet => 'whitepaper three'
> }
>
>
>
> So I am trying to then go like so:
>
> @documentnames[k]
>
> 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]

kwest

6/13/2007 6:38:00 PM

0

OK I answered my own question...sorry. But here is the answer in case
someone else asks the same silly question:

Use a string as a key (doh!)

@documentnames = {
'finance_whitepaper' => 'whitepaper one',
'ecommerce_whitepaper' => 'whitepaper two',
'integratedsvs_datasheet' => 'whitepaper three'
}

Problem solved!


On Jun 13, 11:30 am, kwest <kwes...@gmail.com> wrote:
> Hello,
> I am new to Ruby and have run into a small snag. I have a hash like
> so:
>
> @documentnames = {
> :finance_whitepaper => 'whitepaper one',
> :ecommerce_whitepaper => 'whitepaper two',
> :integratedsvs_datasheet => 'whitepaper three'
> }
>
> So I am trying to then go like so:
>
> @documentnames[k]
>
> 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....


Bira

6/13/2007 6:45:00 PM

0

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:
>
> @documentnames = {
> :finance_whitepaper => 'whitepaper one',
> :ecommerce_whitepaper => 'whitepaper two',
> :integratedsvs_datasheet => 'whitepaper three'
> }
>
>
>
> So I am trying to then go like so:
>
> @documentnames[k]
>
> 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....

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.

--
Bira
http://compexplicita.bl...
http://sinfoniaferida.bl...

kwest

6/13/2007 6:58:00 PM

0

Great thank you. That is too easy. I am liking this.

On Jun 13, 11:36 am, Daniel Kempkens <Daniel.Kempk...@gmail.com>
wrote:
> kwest schrieb:
>
>
>
> > Hello,
> > I am new to Ruby and have run into a small snag. I have a hash like
> > so:
>
> > @documentnames = {
> > :finance_whitepaper => 'whitepaper one',
> > :ecommerce_whitepaper => 'whitepaper two',
> > :integratedsvs_datasheet => 'whitepaper three'
> > }
>
> > So I am trying to then go like so:
>
> > @documentnames[k]
>
> > 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]