C
C
Castiel2015-03-23 18:09:56
ruby
Castiel, 2015-03-23 18:09:56

How to pass global variables to do block?

Get config from file

$app_settings = YAML.load_file("#{File.dirname(__FILE__)}/settings.yml")

then in the class do this
def initialize
    @client = Twitter::REST::Client.new do |config|
      config.consumer_key        = $app_settings[:twitter][:api_key]
      config.consumer_secret     = $app_settings[:twitter][:api_secret]
      config.access_token        = $app_settings[:twitter][:access_token]
      config.access_token_secret = $app_settings[:twitter][:access_token_secret]
    end
  end

but from $app_settings it is not possible to pull out the values. How do you transfer them all?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Viktor Vsk, 2015-03-23
@Castiel

In what class? What do you get? How can you not get it out?
See what's in the variable. Perhaps it is initialized later than the twitter client. Perhaps there is a different structure. Perhaps you need to specify strings instead of characters as keys

D
Dmitry Demin, 2015-03-25
@keksmen

Alternatively, the problem may be in scope. When creating an instance of a class, try to write a reference to the variable into some property of the object.

A
Alexey, 2015-03-26
@fuCtor

After parsing, keys are strings 99% of the time, unless explicitly set to characters:
$app_settings['twitter']['api_key'] - should be like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question