Some help needed with understanding ruby hashes

F

Frank Church

If a ruby hash rubyhash has a key such as "hashkey" is there a way of
retrieving the value by the expression rubyhash["hashkey"] ?

It only seems logical that is rubyhash.has_key?("hashkey") returns true
rubyhash["hashkey"] should return a value, but it always gives an error.

I know the same can be achieved via with rubyhash.values_at("hashkey")
but it appears so inelegant to me.

I am trying to create an erb template which will use the hash keys here
such as directory1 directory2 for each directory and the for each
directory use hash key s such as directory, archivename etc, but I find
values_at rather inelegant.

I sorely hope there is some aspect to ruby hashes syntax that I am
missing.

directories :
directory1 :
directory : /thisdirectory/yada/yada/yada
archivename : archivename01
updatemode : updatemode01
encryptkey : encryptkey01
configdir : configdir01
workdir : workingdir01
storagedir : storagedir01
logsdir : logsdir01

directory2 :
directory : /thisdirectory/yada2/yada2/yada2
archivename : archivename02
updatemode : updatemode02
encryptkey : encryptkey02
configdir : configdir02
workdir : workingdir02
storagedir : storagedir02
logsdir : logsdir02
 
H

Harry Kakueki

If a ruby hash rubyhash has a key such as "hashkey" is there a way of
retrieving the value by the expression rubyhash["hashkey"] ?

It only seems logical that is rubyhash.has_key?("hashkey") returns true
rubyhash["hashkey"] should return a value, but it always gives an error.

This gives you an error?

rubyhash = {"hashkey"=>"hashvalue"}
p rubyhash["hashkey"]


Harry
 
J

James Edward Gray II

If a ruby hash rubyhash has a key such as "hashkey" is there a way of
retrieving the value by the expression rubyhash["hashkey"] ?

It only seems logical that is rubyhash.has_key?("hashkey") returns
true
rubyhash["hashkey"] should return a value, but it always gives an
error.

What you described is the standard Ruby behavior:

$ irb
hash = Hash.new => {}
hash["some_key"] => nil
hash["some_key"] = "I'm set now!" => "I'm set now!"
hash.has_key? "some_key" => true
hash["some_key"]
=> "I'm set now!"

Note how I tried that out in the irb shell. It lets you enter Ruby
expressions and examine their output. This is a powerful ally when
learning the rules of Ruby.

In short, something else is going on in your code. Hashes work as
you expect.

James Edward Gray II
 
M

Morton Goldberg

If a ruby hash rubyhash has a key such as "hashkey" is there a way of
retrieving the value by the expression rubyhash["hashkey"] ?

It only seems logical that is rubyhash.has_key?("hashkey") returns
true
rubyhash["hashkey"] should return a value, but it always gives an
error.

I know the same can be achieved via with rubyhash.values_at("hashkey")
but it appears so inelegant to me.

What you say you are doing works for me.

rubyhash = { "hashkey" => "value"}
rubyhash["hashkey"] # => "value"

I can only conclude that your problem is really other than what you
think it is. I think you would be well advised to post the actual
code that is producing the error.

Regards, Morton
 
F

Frank Church

Frank said:
If a ruby hash rubyhash has a key such as "hashkey" is there a way of
retrieving the value by the expression rubyhash["hashkey"] ?

It only seems logical that is rubyhash.has_key?("hashkey") returns true
rubyhash["hashkey"] should return a value, but it always gives an error.

I know the same can be achieved via with rubyhash.values_at("hashkey")
but it appears so inelegant to me.

I am trying to create an erb template which will use the hash keys here
such as directory1 directory2 for each directory and the for each
directory use hash key s such as directory, archivename etc, but I find
values_at rather inelegant.

I sorely hope there is some aspect to ruby hashes syntax that I am
missing.

I tracked the error, I was testing the using the concatenating strings
without using to_s in the code. ie.

puts "hashkey value is " + rubyhash["hashkey"]

instead of

"hashkey value is " + (rubyhash["hashkey"]).to_s

Regards

Frank
 
M

Morton Goldberg

I tracked the error, I was testing the using the concatenating strings
without using to_s in the code. ie.

puts "hashkey value is " + rubyhash["hashkey"]

instead of

"hashkey value is " + (rubyhash["hashkey"]).to_s

You could have saved yourself a lot trouble by using

puts "hashkey value is #{rubyhash["hashkey"]}"

Regards, Morton
 
F

Frank Church

Morton said:
I tracked the error, I was testing the using the concatenating strings
without using to_s in the code. ie.

puts "hashkey value is " + rubyhash["hashkey"]

instead of

"hashkey value is " + (rubyhash["hashkey"]).to_s

You could have saved yourself a lot trouble by using

puts "hashkey value is #{rubyhash["hashkey"]}"

Regards, Morton

I am learning Ruby step by step. There appear to be so many different
ways of achieving the same thing, coming from a more procedural
background, there are quite a number of things about the Ruby way I have
to learn.

As you can see from here
http://forums.devshed.com/ruby-programming-142/erb-undefined-local-variable-or-method-446086.html

I have an even more troublesome question I can't locate. I will post it
when I find it.
 
M

Morton Goldberg

Morton said:
You could have saved yourself a lot trouble by using

puts "hashkey value is #{rubyhash["hashkey"]}"

Regards, Morton

I am learning Ruby step by step. There appear to be so many different
ways of achieving the same thing, coming from a more procedural
background, there are quite a number of things about the Ruby way I
have
to learn.

The point to note about 'puts "hashkey value is #{rubyhash
["hashkey"]}"' is that the interpreter calls to_s on the result of a #
{...} evaluation, but String#+ is picky about is argument -- the
argument must be a string. That means, as you have found out, that
you must explicitly coerce anything not a string with to_s before
passing it to String#+.

Regards, Morton
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top