patternrubyMinor
The Badge Bot 9000
Viewed 0 times
badge9000thebot
Problem
Because I'm both lazy and wanting to mess around more with time management and Ruby, I thought it would be fun to create a simple script that opens Code Review once every day to go towards the daily login badge awards! There are so many ways to go about this that I'm certain you could probably do this within several lines of code (especially with Ruby).
I'm also aware that there's no code to handle closing the browser. This is handled manually, because as a software developer I'd be ashamed if I didn't attend the computer once at some point in the day.
require 'launchy'
module BadgeBot
def at_time(time)
loop do
before = Time.now
yield
interval = time - (Time.now - before)
sleep interval if interval > 0
end
end
# every 23 hours, open the webpage
at_time(82800) do
Launchy.open('http://codereview.stackexchange.com/')
end
endI'm also aware that there's no code to handle closing the browser. This is handled manually, because as a software developer I'd be ashamed if I didn't attend the computer once at some point in the day.
Solution
It's good.
I would make the time_to_wait a constant instead of a number and write it as
or much more clearly but also more verbosely:
And then
And
^ Looks like premature optimization, sleeping 0 just does nothing, so you may just use
Which advantages does
I would make the time_to_wait a constant instead of a number and write it as
TIME_TO_WAIT = 23 60 60or much more clearly but also more verbosely:
def hours_to_seconds(hours)
hours * 60 * 60
endAnd then
TIME_TO_WAIT = hours_to_seconds(23)And
sleep interval if interval > 0^ Looks like premature optimization, sleeping 0 just does nothing, so you may just use
sleep intervalWhich advantages does
module give you? It is sound to use a name-space for importable scripts, but this one is only ever going to be run as a stand-alone, so you may just remove it.Code Snippets
def hours_to_seconds(hours)
hours * 60 * 60
endTIME_TO_WAIT = hours_to_seconds(23)sleep interval if interval > 0sleep intervalContext
StackExchange Code Review Q#105519, answer score: 4
Revisions (0)
No revisions yet.