Ruby - 未定义的方法`extract_options!' :数组

Ami*_*tel 3 ruby ruby-on-rails tweetstream ruby-on-rails-4

使用TweetStream运行以下示例时,我得到提到的错误.

tweets.rb

require 'tweetstream'

TweetStream.configure do |config|
  config.consumer_key       = '<CONSUMER KEY>'
  config.consumer_secret    = '<CONSUMER SECRET>'
  config.oauth_token        = '<OAUTH TOKEN>'
  config.oauth_token_secret = '<OAUTH TOKEN SECRET'
  config.auth_method        = :oauth
end

TweetStream::Client.new.track('ruby') do |status|
  puts "#{status.text}"
end
Run Code Online (Sandbox Code Playgroud)

错误

$ ruby tweets.rb 
/home/amit/.rvm/gems/ruby-1.9.3-p194/gems/tweetstream-2.3.0/lib/tweetstream/client.rb:96:in `track': undefined method `extract_options!' for ["ruby"]:Array (NoMethodError)
        from tweets.rb:11:in `<main>'
    https://github.com/intridea/tweetstream
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?

K M*_*lam 7

这是另一个解决方案:打开Array课程并extract_options!在其上定义方法.

添加以下代码:

class Array
  def extract_options!
    last.is_a?(::Hash) ? pop : {}
  end unless defined? Array.new.extract_options!
end
Run Code Online (Sandbox Code Playgroud)

tweets.rb文件的开头或单独的文件(文件中需要它tweets.rb).

  • 我喜欢这个解决方案.如果您不需要其他任何东西,包括ActiveSupport是不可取的. (2认同)