small medium large xlarge

Generic-user-small
25 Oct 2017, 05:56
Brandon Pittman (5 posts)

The instance_eval example doesn’t seem to work.

module Twitter
  module REST
    class Client
      attr_accessor :consumer_key, :consumer_secret,     # => :consumer_secret
                    :access_token, :access_token_secret  # => nil

      def initialize(options = {}, &block)
        options.each { |k,v| send("#{k}=", v) }  # => {:consumer_key=>"YOUR_CONSUMER_KEY"}
        instance_eval(&block) if block_given?    # => "YOUR_ACCESS_SECRET"
      end                                        # => :initialize
    end                                          # => :initialize
  end                                            # => :initialize
end                                              # => :initialize

client = Twitter::REST::Client.new({consumer_key: "YOUR_CONSUMER_KEY"}) do  # => Twitter::REST::Client
  consumer_secret     = "YOUR_CONSUMER_SECRET"                              # => "YOUR_CONSUMER_SECRET"
  access_token        = "YOUR_ACCESS_TOKEN"                                 # => "YOUR_ACCESS_TOKEN"
  access_token_secret = "YOUR_ACCESS_SECRET"                                # => "YOUR_ACCESS_SECRET"
end                                                                         # => #<Twitter::REST::Client:0x00007ffbd2145778 @consumer_key="YOUR_CONSUMER_KEY">

p client.consumer_key  # => "YOUR_CONSUMER_KEY"
p client.access_token  # => nil

# >> "YOUR_CONSUMER_KEY"
# >> nil

As you can see, the access_token isn’t getting set. As far as I can tell, it’s just setting a local variable inside the block.

Generic-user-small
11 Dec 2017, 02:39
rajas@waddleworks.com (1 post)

I second this. I’m getting identical results. Fantastic book by the way. Also, am I crazy, or is the explanation for the SpiceGirl example missing from the back pages?

You must be logged in to comment