0

Rails 4 + Redis 3.0.4

Redis でチャンネルを購読しようとすると:

$ rails c
irb(main):001:0> redis = Redis.new
=> #<Redis client v3.0.2 for redis://127.0.0.1:6379/0>
irb(main):002:0> redis.subscribe('test-channel')

エラーがスローされます:

LocalJumpError: no block given (yield)
from .rvm/gems/ruby-1.9.3-p392/gems/redis-3.0.4/lib/redis/subscribe.rb:52:in `initialize'
from .rvm/gems/ruby-1.9.3-p392/gems/redis-3.0.4/lib/redis/subscribe.rb:30:in `new'
from .rvm/gems/ruby-1.9.3-p392/gems/redis-3.0.4/lib/redis/subscribe.rb:30:in `subscription'
from .rvm/gems/ruby-1.9.3-p392/gems/redis-3.0.4/lib/redis/subscribe.rb:12:in `subscribe'
from .rvm/gems/ruby-1.9.3-p392/gems/redis-3.0.4/lib/redis.rb:2221:in `_subscription'
from .rvm/gems/ruby-1.9.3-p392/gems/redis-3.0.4/lib/redis.rb:1883:in `block in subscribe'
from .rvm/gems/ruby-1.9.3-p392/gems/redis-3.0.4/lib/redis.rb:36:in `block in synchronize'
from .rvm/rubies/ruby-1.9.3-p392/lib/ruby/1.9.1/monitor.rb:211:in `mon_synchronize'
from .rvm/gems/ruby-1.9.3-p392/gems/redis-3.0.4/lib/redis.rb:36:in `synchronize'
from .rvm/gems/ruby-1.9.3-p392/gems/redis-3.0.4/lib/redis.rb:1882:in `subscribe'
from (irb):2
from .rvm/gems/ruby-1.9.3-p392/gems/railties-4.0.0/lib/rails/commands/console.rb:90:in `start'
from .rvm/gems/ruby-1.9.3-p392/gems/railties-4.0.0/lib/rails/commands/console.rb:9:in `start'
from .rvm/gems/ruby-1.9.3-p392/gems/railties-4.0.0/lib/rails/commands.rb:64:in `<top (required)>'
from script/rails:6:in `require'
from script/rails:6:in `<main>'

私が間違っているのは何ですか?

4

1 に答える 1

3

ワイルドな推測は、サブスクライブがブロックを期待していることです。

したがって、ブロックを渡します。ヒント: do end ブロックを使用します。

このような:

$redis.subscribe('rubyonrails', 'ruby-lang') do |on|   
  on.message do |channel, msg|
   data = JSON.parse(msg)
   puts "##{channel} - [#{data['user']}]: #{data['msg']}"   
  end 
end
于 2013-08-19T12:01:54.877 に答える