user647527 user647527 - 1 month ago 8
Ruby Question

How to call redis.evalsha

I faced a doubt on how to use r.evalsha under ruby. I able to get result on CLI to call redis. Error shown me: attempt to concatenate local 'nid' (a boolean value)

Please advise me .

Thank You.

evalsha 0ca2da3beea9739f3d61798a36bd4d5ed56754ee 1 10.17.103.200


The testredis.rb like at following:

require 'rubygems'
require 'redis'
r = Redis.new(:host => '127.0.0.1', :port => '6379' )
$result = r.evalsha("0ca2da3beea9739f3d61798a36bd4d5ed56754ee", ['1'], ['10.17.103.200'])
puts $result
r.quit()

Answer

The ruby redis gem attempts to clean up some things for you. You don't need to pass the number of arguments. The second argument to evalsha is an array of script arguments. Your evalsha line should look like this:

$result = r.evalsha("0ca2da3beea9739f3d61798a36bd4d5ed56754ee", ['10.17.103.200'])

To load a script from within ruby you can use Redis#script:

sha = r.script(:load, "return redis.call('incr', 'foo')")
r.evalsha(sha) # => 1
r.evalsha(sha) # => 2