Skip to content
This repository has been archived by the owner on Jul 5, 2023. It is now read-only.

HGETALL returns array instead of hash #13

Open
raphaottoni opened this issue Jun 19, 2015 · 4 comments
Open

HGETALL returns array instead of hash #13

raphaottoni opened this issue Jun 19, 2015 · 4 comments

Comments

@raphaottoni
Copy link

Acessing the key from redis-rb gem using hgetall return a hash:

screen shot 2015-06-18 at 23 59 44

when accessing the same key with a lua script using wolverine it returns a array as shown:

screen shot 2015-06-18 at 23 57 37

hmget.lua:

screen shot 2015-06-19 at 00 02 32

Already tested, it was not messagePack fault =/

@yaauie
Copy link

yaauie commented Jun 19, 2015

The redis-rb gem has special instructions to convert the response in Redis#hgetall into a ruby hash, but those instructions aren't present on the generic Redis#evalscript or Redis#evalsha commands, so you'll need to convert it to a ruby hash yourself.

@raphaottoni
Copy link
Author

Thanks for your answer @yaauie !

If I may ask, what do you think would be more time efficient:

  1. Save the value as a ruby's hash in form of string and than use eval to transform it
    2 ) Split that hash into multiple gets, each with a single value
  2. Save the value as a json object in form of string and than use oj gem to load it

I also created a stackoverflow question about this HGETALL issue, if you don't mind, it would be nice to have your answer there.
http://stackoverflow.com/questions/30929947/hgetall-returns-array-instead-of-hash-when-using-lua-scripting-rails-redis-lua

Really thanks for the help

@yaauie
Copy link

yaauie commented Jun 19, 2015

None of those; use Hash[] to convert the Array response into a Hash:

response = ["an", "array", "with", "an", "even", "number", "of", "entries"]
#=>  ["an", "array", "with", "an", "even", "number", "of", "entries"]
a_hash = Hash[*response]
#=>  {"an"=>"array","with"=>"an", "even"=>"number", "of"=>"entries"]

@raphaottoni
Copy link
Author

Thank @yaauie !

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants