New to ruby - need assistance with rewriting a script

Hello,

I’m working on learning ruby and trying to figure out how I would take
an existing example and rewrite it in a way that suits my own usage.
What I’m looking for here isn’t for someone to just give me a block of
fixed code and be like, “Oh, here you go!” since I won’t actually learn
anything from that.

So, here’s the script:

require ‘rubygems’
require ‘crontab-parser’

start_time = Time.parse(ARGV[0])
end_time = Time.parse(ARGV[1])

them
cron_data = File.readlines(ARGV[2]).select {|line| line =~ /^[0-9*]/}
cron = CrontabParser.new(cron_data.join("\n"))
(start_time…end_time).each do |timestamp|
next unless timestamp.sec.to_i == 0
cron.each do |cron_entry|
if cron_entry.should_run?(timestamp)
puts “#{timestamp}\t#{cron_entry.cmd}”
end
end
end

Again, not my code. What I want to do is like so:

  1. Run this script without requiring any CLI arguments. I know the time
    interval I want to search for (between 1 and 14 minutes) but I don’t
    want to have to type out the range of times when I execute the script.
    I’ve seen the chronic gem and believe this is the answer I’m looking
    for, but not certain of how I’d integrate that in here.

  2. Scan all of the files in a directory with the exception of two (of
    which I’d need to set an .excludes). Something like this (my code):

Dir["/var/spool/cron/*"].each do |file|
username = file.split("/").last
crontab = CrontabParser.new(File.read(file))

crontab.each do |cron|
puts “User: #{username}”
# Do whatever
end
end

This way I can get the output of:

User1
cron1
cron2

User2
Cron1
Cron2

And so on.

Can anyone offer some sort of assistance in helping me get this to where
I want it? I’m happy with any kind of relevant documentation for this
task (there’s so much unrelated documentation that I end up
lost/confused), examples that are similar in what I’m trying to do, or
partial
answers that will point me in the right direction.

I know this may seem a bit unorthodox, but it’s how I learn - I can’t
just read a book on ruby and understand how to do everything (I’ve read
Chis Pine’s book and it has helped a little, but not enough for me to be
able to do something like this on my own).

Thanks in advance!

On Fri, Feb 8, 2013 at 9:00 AM, Ronald C. [email protected]
wrote:

next unless timestamp.sec.to_i == 0

  1. Run this script without requiring any CLI arguments. I know the time
    username = file.split("/").last

Can anyone offer some sort of assistance in helping me get this to where

Thanks in advance!


Posted via http://www.ruby-forum.com/.

You might want to look at TImeCop in association with Chronic.

I’m not sure how TimeCop would be useful in this situation. It doesn’t
look to have any real reason for me to use it, and chronic_duration
should fulfill what I’m attempting to do without any issues.

On Mon, Feb 11, 2013 at 8:11 AM, Ronald C. [email protected]
wrote:

I’m not sure how TimeCop would be useful in this situation. It doesn’t
look to have any real reason for me to use it, and chronic_duration
should fulfill what I’m attempting to do without any issues.

Chronic only gives you the next time in the formula, doesn’t it? It
sounded like you wanted to know when all the times were for a given
span so you could pick off the ones that fire between 1 and 14 minutes
in the hour? I’m sure I really don’t understand what you’re wanting to
do. Glad you have what you want.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs