diff options
| author | Joe Robinson <joe@lc8n.com> | 2015-10-09 20:28:06 +0100 | 
|---|---|---|
| committer | Joe Robinson <joe@lc8n.com> | 2015-10-09 20:28:06 +0100 | 
| commit | d372de14d696fe66d9bc6f1ffb78b64211101374 (patch) | |
| tree | 885eedc748eb863a39f5a09b16fbf42352a6855e /blaunits.rb | |
Initial commit. Basic conversion
Diffstat (limited to 'blaunits.rb')
| -rwxr-xr-x | blaunits.rb | 75 | 
1 files changed, 75 insertions, 0 deletions
| diff --git a/blaunits.rb b/blaunits.rb new file mode 100755 index 0000000..460df9a --- /dev/null +++ b/blaunits.rb @@ -0,0 +1,75 @@ +#!/usr/bin/ruby + +VERSION = "2.0" + +begin +    args = ARGV.dup +    if args.empty? +       input = STDIN.gets +    else +        input = "" +        args.each do |arg| +            if input != "" +            input = input + " " + arg.dup +            else +                input = arg.dup +            end +        end +    end +    input.strip! +    if input == "-v" or input ==  "--version" +        puts "blaconvert v"+VERSION +        exit +    end +    mid = 0 +    from = "" +    to = "" +    num = 0 +    words = input.split(" ") +    for i in 0..words.length-1 +        if words[i] == "in" or words[i] == "to" +            mid = i +        end +    end +    if words.length == 3 && mid == 0 +        num = Float(words[0]) +        from = words[1] +        to = words[2] +    elsif words.length == 2 && mid == 0 +    	num = 1 +    	from = words[0] +    	to = words[1] +    elsif mid > 0 +    	for i in 1..mid-1 +    		from += words[i] +    	end +    	for i in mid+1..words.length-1 +    		to += words[i] +    	end +    	num = Float(words[0]) +    else +    	puts "Invalid input! Try !convert [amount] [from-unit] to [to-unit] e.g !convert 2 cm to inches" +    	exit +    end +    out = %x(gunits -t1 #{from} #{to}) +    outs = out.split(" ") +    if outs[0] == "Unknown" +    	from.upcase! +    	out = %x(gunits -t1 #{from} #{to}) +	    outs = out.split(" ") +	    if outs[0] == "Unknown" +	    	to.upcase! +	    	out = %x(gunits -t1 #{from} #{to}) +	    	outs = out.split(" ") +	    	if outs[0] == "Unknown" +	    		puts out +	    		exit +	    	end +	    end +    elsif outs[0] == "conformability" +    	puts "Incompatible units. Can't convert from #{from} to #{to}" +    	exit +    end +    puts "#{num} #{from} = #{num * Float(out)} #{to}" +	 +end
\ No newline at end of file | 
