Why doesn't my Ruby script run when I've moved the code to functions?
I've a simple script which basically reads a RSS script & pushes it to twitter.
The script works fine, except when I move it all to functions and call the functions, it doesn't execute. Why is this so?
Here's what works:
require 'rubygems'
require 'simple-rss'
require 'open-uri'
require 'bitly'
require 'twitter'
def setup_bitly()
puts "Bitly setup"
bitly_username = "<user-name>"
bitly_apikey = "R_<api-key>"
Bitly.use_api_version_3
bitly = Bitly.new(bitly_username, bitly_apikey)
end
def setup_twitter()
Twitter.configure do |config|
config.consumer_key = "<key>"
config.consumer_secret = "<secret>"
config.oauth_token = "<token>"
config.oauth_token_secret = "<secret>"
twitter = Twitter::Client.new
end
end
bitly = setup_bitly()
twitter = setup_twitter()
url = "http://stackoverflow.com/feeds/tag/ruby"
puts 'Fetching and parsing feed: ' + url.to_s
rss = SimpleRSS.parse open(url)
rss.items.reverse.each_with_index do |i,idx|
u = bitly.shorten(i.link, :history => 1)
twitter_status = "#{i.开发者_如何学编程title}"
puts twitter_status
twitter.update(twitter_status)
sleep 10
end
Here's the output:
C:\Users\Sathya\Documents\code\read-feed>read-feed.rb
Fetching and parsing feed: http://stackoverflow.com/feeds/tag/ruby
Dynamic lists in rails 3
Ruby add dynamic events using AASM
If I move the last block to a function, it doesn't execute. By "doesn't execute", I mean, the Ruby interpreter runs & quits, no (error) messages, nothing. Why is this so?
This is the version which is causing problems:
require 'rubygems'
require 'simple-rss'
require 'open-uri'
require 'bitly'
require 'twitter'
def setup_bitly()
puts "Bitly setup"
bitly_username = "<username>"
bitly_apikey = "<api_key>"
Bitly.use_api_version_3
bitly = Bitly.new(bitly_username, bitly_apikey)
end
def setup_twitter()
Twitter.configure do |config|
config.consumer_key = "<key>"
config.consumer_secret = "<secret>"
config.oauth_token = "<token>"
config.oauth_token_secret = "<secret>"
twitter = Twitter::Client.new
end
end
def do_work()
url = "http://stackoverflow.com/feeds/tag/ruby"
puts 'Fetching and parsing feed: ' + url.to_s
rss = SimpleRSS.parse open(url)
rss.items.reverse.each_with_index do |i,idx|
u = bitly.shorten(i.link, :history => 1)
twitter_status = "#{i.title}"
puts twitter_status
twitter.update(twitter_status)
sleep 10
end
bitly = setup_bitly()
twitter = setup_twitter()
do_work()
end
If I run this, the script runs & quits:
C:\Users\Sathya\Documents\code\read-feed>read-feed.rb
I'm running on Windows 7 64-bit on Ruby 1.9.2p290
This is perfectly normal, your function doesn't see the variables bitly
and twitter
.
You can do one the following:
- Pass them as arguments to your function.
- Wrap the function in a class and use @instance variables.
- Use global variables (please do not :) )
Are you sure this runs quietly without errors? There are at least 2 problems with the code as is:
You are calling
twitter.update
indo_work
without having a local variable calledtwitter
in scope. Same goes forbitly
.There's an extra
end
in the last line.
精彩评论