Skip to content

Instantly share code, notes, and snippets.

@vtim
Last active December 11, 2015 08:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save vtim/4572698 to your computer and use it in GitHub Desktop.
Save vtim/4572698 to your computer and use it in GitHub Desktop.
My first ruby script (for: http://wolfslittlestore.be/2013/01/looking-for-shell-programming-expert/) Usage in terminal: ./billing.rb < input.txt > output.txt
#!/usr/bin/ruby
require 'date'
reLine1 = Regexp.new(/\[b([\d,]+)h\] (.*)/)
reLine2 = Regexp.new(/scheduled (.*)/)
@@hours = nil
@@title = nil
@@date = nil
@@totalHours = 0
STDIN.each_line { |line|
match = reLine1.match(line)
if match
@@hours = match[1]
@@title = match[2]
else
match = reLine2.match(line)
if match
@@date = Date.parse(match[1])
@@totalHours += @@hours.tr(",", ".").to_f
puts "#{@@date.strftime('%d-%m-%y')}\t#{@@hours}\t#{@@title}"
end
end
}
puts "\nTotaal: #{@@totalHours}"
[b4h] WORK @ Sample project
scheduled 24 Oct 2012 from 08:00 to 12:00
[b3,5h] Sample project
scheduled 5 Nov 2012 from 08:30 to 12:00
24-10-12 4 WORK @ Sample project
05-11-12 3,5 Sample project
Totaal: 7.5
@Wolfr
Copy link

Wolfr commented Jan 19, 2013

Thanks Tim :)
I hope you did this from a learning point of view, I might go with another solution because my own Ruby skills are nonexistant

will also answer to https://gist.github.com/4572970

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment