Ruby为nil提供了“NoMethodError:undefined method` []':NilClass”和Sinatra

时间:2014-03-22 17:27:43

标签: ruby sinatra

我是Ruby和Sinatra的全新手,我正在努力学习它们。

所以这是我的ruby代码(File:ruby_test2.rb)

#!/usr/bin/ruby
require 'rubygems'

class LearnRuby

  def initalize
    @listen = Hash.new { |hash, key| hash[key] = []; }
  end

  def fill_listen(uid, mid)
    @listen[uid]<<=mid
    "In fill_listen #{@listen}\n"
  end

end

这是我的Sinatra代码(文件:sinatra_test2.rb)

require 'sinatra'
require './ruby_test2'

if __FILE__ == $0
  lr = LearnRuby.new
end

post '/listen' do
  lr.fill_listen params[:uid] , params[:mid]
end

这就是我运行它的方式。

ruby sinatra_test2.rb&lt; - 启动服务器

curl --data "uid=u1&mid=m312" http://localhost:4567/listen&lt; - 发布请求的卷曲命令

当我执行上述操作时,我收到此错误(在服务器和客户端(curl)端)

localhost - - [22/Mar/2014:22:54:19 IST] "POST /listen HTTP/1.1" 500 3770
- -> /listen
NoMethodError - undefined method `[]' for nil:NilClass:
    <path>/ruby_test2.rb:11:in `fill_listen'
    sinatra_test2.rb:9:in `block in <main>'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1593:in `call'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1593:in `block in compile!'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:957:in `[]'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:957:in `block (3 levels) in route!'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:976:in `route_eval'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:957:in `block (2 levels) in route!'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:997:in `block in process_route'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:995:in `catch'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:995:in `process_route'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:955:in `block in route!'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:954:in `each'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:954:in `route!'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1067:in `block in dispatch!'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `block in invoke'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `catch'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `invoke'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1064:in `dispatch!'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:889:in `block in call!'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `block in invoke'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `catch'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1049:in `invoke'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:889:in `call!'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:877:in `call'
    /var/lib/gems/1.9.1/gems/rack-protection-1.5.2/lib/rack/protection/xss_header.rb:18:in `call'
    /var/lib/gems/1.9.1/gems/rack-protection-1.5.2/lib/rack/protection/path_traversal.rb:16:in `call'
    /var/lib/gems/1.9.1/gems/rack-protection-1.5.2/lib/rack/protection/json_csrf.rb:18:in `call'
    /var/lib/gems/1.9.1/gems/rack-protection-1.5.2/lib/rack/protection/base.rb:50:in `call'
    /var/lib/gems/1.9.1/gems/rack-protection-1.5.2/lib/rack/protection/base.rb:50:in `call'
    /var/lib/gems/1.9.1/gems/rack-protection-1.5.2/lib/rack/protection/frame_options.rb:31:in `call'
    /var/lib/gems/1.9.1/gems/rack-1.5.2/lib/rack/logger.rb:15:in `call'
    /var/lib/gems/1.9.1/gems/rack-1.5.2/lib/rack/commonlogger.rb:33:in `call'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:217:in `call'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:210:in `call'
    /var/lib/gems/1.9.1/gems/rack-1.5.2/lib/rack/head.rb:11:in `call'
    /var/lib/gems/1.9.1/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/show_exceptions.rb:21:in `call'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:180:in `call'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:2004:in `call'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1469:in `block in call'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1778:in `synchronize'
    /var/lib/gems/1.9.1/gems/sinatra-1.4.4/lib/sinatra/base.rb:1469:in `call'
    /var/lib/gems/1.9.1/gems/rack-1.5.2/lib/rack/handler/webrick.rb:60:in `service'
    /usr/lib/ruby/1.9.1/webrick/httpserver.rb:138:in `service'
    /usr/lib/ruby/1.9.1/webrick/httpserver.rb:94:in `run'
    /usr/lib/ruby/1.9.1/webrick/server.rb:191:in `block in start_thread'
localhost - - [22/Mar/2014:22:54:51 IST] "POST /listen HTTP/1.1" 500 3770

我可能会错过一些小事,但我需要帮助,因为我刚开始学习这些技术。

提前致谢。

2 个答案:

答案 0 :(得分:2)

以下是您的工作示例,@listen为零,因为您拼写错误initalize - &gt; initialize

require 'sinatra'

class LearnRuby
  def initialize
    @listen = {}
  end

  def fill_listen(uid, mid)
    @listen[uid] = mid
    "In fill_listen #{ @listen }\n"
  end
end

post '/listen' do
  lr = LearnRuby.new
  lr.fill_listen(params[:uid], params[:mid])
end

答案 1 :(得分:0)

@listen[uid]<<=mid替换为@listen[uid] = mid

此外,如果您只需要initialize来创建哈希,那么您可以将@listen = Hash.new { |hash, key| hash[key] = []; }替换为@listen = {}