4

While running following sample using TweetStream I am getting mentioned error.

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

Error

$ 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

Am I missing something?

K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
Amit Patel
  • 15,609
  • 18
  • 68
  • 106

3 Answers3

7

Here's another solution: opening up Array class and defining extract_options! method on it.

Add the following code :

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

to the beginning of the tweets.rb file or to a separate file (which would need to be required in the tweets.rb file).

K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
  • 2
    I Like this solution. Including ActiveSupport if you don't need it for anything else is not the way to go. – thomax Oct 31 '13 at 09:02
  • 1
    [Rails implementation](https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/array/extract_options.rb) – notapatch Nov 11 '15 at 10:44
3

extract_options! is ActiveSupport method. If it's not rails app you need to install it and include into script.

Sigurd
  • 7,865
  • 3
  • 24
  • 34
  • 1
    Thanks. It worked. Actually [this commit message](https://github.com/intridea/tweetstream/commit/a63942dec7099efc74bc3d6bdae76a287de1d0ef) was stopping me to explore elsewhere in rails. – Amit Patel Dec 14 '12 at 08:16
1

I am too late to answer but i think it'll be useful for ruby naive programmers like me.

To include ActiveSupport method like extract_options!, you need to include Active Support.

require 'active_support'

And if you want to include ruby gems then include rubygems too.

require 'rubygems'

Radix
  • 2,527
  • 1
  • 19
  • 43