I have a ruby hash(@@PATH) where each element consists of a string MD5
as the key, and the physical path of the MD5 as the value.
In the above code I aspire to retrieve the value(the MD5 path) by using
a variable(VAR) which holds the md5 value. The reason why I cant
directly call the value using a string is because the above code
iterates through a loop where VAR changes.
Basically what I am trying to achieve is a database showing a file MD5
with matching physical path.
Therefore i ask is it even possible to call a ruby hash element through
a variable?
Therefore i ask is it even possible to call a ruby hash element through
a variable?
Yes, and it works just like you tried above.
However, variables mustn't begin with an uppercase letter, this is
reserved for constants. So you have to use "var" instead of "VAR".
And I'm wondering why you use "MD5" in one case and "VAR" in the other.
Shouldn't both be the same? Also I don't think a class variable (double
@@) is really what you want.
Thanks for you quick reply. The VAR-MD5 was a typo. sorry. The class
variable and variable case will be looked into.
However when I utilize the above format for calling the element in the
hash I receive
'+' can't convert nil into String (TypeError)
which is very strange since the variable does hold a md5 in string
format. the hash generates this error unless the element is called
directly with an md5 string.
which is very strange since the variable does hold a md5 in string
format. the hash generates this error unless the element is called
directly with an md5 string.
Then the string in the md5 variable doesn't exactly match the hash keys.
Check this by inspecting the variable and comparing it to the hash keys:
p md5
If you don't find the error yourself, please post the result of the
expression above and an example key of the hash:
p @@PATH.keys.first
Maybe one of the md5 Strings is actually a byte sequence, while the
other one consists of hex digits.