Ruby Duration

[download id="1" format="1"]

Duration.new recieves a string describing a duration of time, and converts it into: seconds, and a standard readable format.
Or, you can pass Duration.new an integer representing seconds, and receive the same standard readable format.

Usage:

Passing a String

duration = Duration.new("2weeks 8 hr 30m")
duration.seconds  # => 1240200
duration.readable  # => "2 weeks, 8 hours and 30 mins"

Passing an Fixnum (of seconds)

duration = Duration.new(1240200)
duration.readable  # => "2 weeks, 8 hours and 30 mins"

Example valid input strings
“2 weeks”, “2wks”, “2w”,
“8h”, “8 hrs”,”8 hours”,
“30m”, “0:30″, “30 minutes”, “30min”,
“2 weeks, 8 hours and 30 minutes”, “2w 8h 30m”, “2w 8:30″,
“4 hours 30 minutes”, “4 hours and 30m”, “4h 30min”, “4:30″, “4.5″, “4.50″, “4h, 30 min”, “4.5 hours”, “4.50h”

http://ahabman.com added functionallity, extended and mashed-up http://stackoverflow.com/questions/657309/how-to-parse-days-hours-minutes-seconds-in-ruby and http://www.postal-code.com/binarycode/2007/04/04/english-friendly-timespan/

1 Comment

ruby csv to structured hash

I had a csv like this:

object color flavor shape
apple red sweet round
banana yellow sweet long
lemon yellow sour round

and I wanted a ruby hash structured like this:

{
'apple'=> { 'color'=>'red',  'flavor'=>'sweet', 'shape'=>'round'},
'banana'=> {  'color'=>'yellow',  'flavor'=>'sweet', 'shape'=>'long'},
'lemon'=> {   'color'=>'yellow',  'flavor'=>'sour', 'shape'=>'round'}
}

So I wrote this, which does the job:

require "faster_csv"

def csv_to_structured_hash
	arr_of_arrs = FasterCSV.read( 'your.csv' )
	stuff = {}
	header = arr_of_arrs.shift
		arr_of_arrs.each_with_index do |row, i|
		thing = { row[0] => {} }
			header.each_with_index do |col, header_index|
			thing[ row[0] ][ header[header_index] ] = row[ header_index]
			end
		stuff.update( thing )
		end
	return stuff
end
Leave a comment

Ruby conversion module

This is a ruby module useful for conversions dealing with length, weight, torque.

 module Convert 

   def Convert.number_with_precision(number, precision=2)
     "%01.#{precision}f" % number
   rescue
     number
   end 

   def Convert.mm_to_in(mm, precision=2)
     number_with_precision(mm * 0.03937, precision)
   end 

   def Convert.in_to_mm(inches, precision=2)
     number_with_precision(inches / 0.03937 , precision)
   end 

   def Convert.feet_to_meters(f, precision=2)
     number_with_precision( f * 0.3048, precision)
   end 

   def Convert.meters_to_inches(m, precision=2)
     number_with_precision( m * 39.37, precision)
   end 

   def Convert.meters_to_feet(m, precision=2)
     number_with_precision( m * 3.281, precision)
   end 

   def Convert.kg_to_lbs(kg, precision=2)
     number_with_precision( kg * 2.2   , precision)
   end 

   def Convert.lbs_to_kg(lbs, precision=2)
     number_with_precision( lbs / 2.2   , precision)
   end 

   def Convert.nm_to_inch_pounds(nm, precision=1)
     number_with_precision( nm * 8.850   , precision)
   end 

   def Convert.inch_pounds_to_nm(inlb, precision=1)
     number_with_precision( inlb / 8.850   , precision)
   end 

 end
Leave a comment