Skip to content

Instantly share code, notes, and snippets.

@apeiros
Forked from KaffeJunky123/liquid-calc.rb
Created July 21, 2018 15:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save apeiros/b5f7f16a770fa3d49d6502737516b885 to your computer and use it in GitHub Desktop.
Save apeiros/b5f7f16a770fa3d49d6502737516b885 to your computer and use it in GitHub Desktop.
A simple application to calculate an e-liquid mixture
# @example
# vapey = Vapey.new
# vapey.read_amounts
# puts vapey
class Vapey
Valid = {
"ml" => ->(ml) { Integer(ml, 10) rescue false },
"mg" => ->(mg) { Integer(mg, 10) rescue false },
"percent" => ->(pc) { Float(pc).between?(0, 1) rescue false },
}
Convert = {
'ml' => ->(ml) { Integer(ml) },
'mg' => ->(mg) { Integer(mg) },
'percent' => ->(pc) { Float(pc) },
}
PrintableTemplate = <<~EOS
Total amount: %dml
Base Amount: %fml(total) %fml(VG) %fml(PG)
Aroma Amount: %fml
Amount Shot: %fml
EOS
def initialize
@out = $stdout
@in = $stdin
@ml_total = 100
@aroma_percent = 0.05
@nicotin_per_ml = 3
@shot_concentration = 18
@target_vg = 0.75
end
def read_amounts
@ml_total = read_unit('total', 'ml')
@aroma_percent = read_unit('aroma', 'percent')
@nicotin_per_ml = read_unit('target nicotine content', 'mg')
@shot_concentration = read_unit('shot nicotine content', 'mg')
@target_vg = read_unit('target VG', 'percent')
end
def to_s
amount_shot = @ml_total.fdiv(@shot_concentration) * @nicotin_per_ml
amount_aroma = @ml_total * @aroma_percent
amount_base = @ml_total - amount_shot - amount_aroma
base_vg = (amount_base) - (amount_shot+amount_aroma) / 2.0
base_pg = amount_base - base_vg
sprintf(
PrintableTemplate,
@ml_total,
base_vg+base_pg,
base_vg, base_pg,
amount_aroma,
amount_shot
)
end
private def read_unit(name, unit)
@out.print "Enter #{name} in #{unit}: "
@out.flush
begin
input = @in.gets.chomp
valid_input = Valid[unit].call(input)
@out.puts "invalid input" unless valid_input
end until valid_input
Convert[unit].call(input)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment