rubychem 1.0.3 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2010 Justin Zollars
1
+ Copyright (c) Justin Zollars
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.markdown CHANGED
@@ -1,25 +1,18 @@
1
1
  # RubyChem - scripting chemistry intelligence
2
2
 
3
- Example:
4
-
5
- * H2SO4 -> ["H2", "S", "O4"]
6
- * -> 97.99
7
-
8
3
  ************************************************************************
9
4
 
10
5
  # Installation
11
6
 
12
- * gem install rubychem
7
+ gem install rubychem
13
8
 
14
9
 
15
10
  # Use
16
-
17
- * x = RubyChem::Valence.new(1).calc_valence
18
- * => {"1s"=>1}
19
-
20
- * x = RubyChem::Chemical.new("H2O")
21
- * => #<RubyChem::Chemical:0x00000101059540 @chem_species=[["H", "2"], ["O", "1"]], @mm=18.01, @moles=.0.010205122971731808>
22
- * x = RubyChem::Chemical.new("H2O").chem_species
23
- * => [["H", "2"], ["O", "1"]]
24
- * x = RubyChem::Chemical.new("H2O",1).moles
25
- * => 0.010205122971731808
11
+ x = RubyChem::Valence.new(1).calc_valence
12
+
13
+ x = RubyChem::Chemical.new("H2O")
14
+ => #<RubyChem::Chemical:0x00000101059540 @chem_species=[["H", "2"], ["O", "1"]], @mm=18.01, @moles=.0.010205122971731808>
15
+ x = RubyChem::Chemical.new("H2O").chem_species
16
+ => [["H", "2"], ["O", "1"]]
17
+ x = RubyChem::Chemical.new("H2O",1).moles
18
+ => 0.010205122971731808`
data/lib/rubychem.rb CHANGED
@@ -1,2 +1,3 @@
1
1
  load 'rubychem/molecule.rb'
2
- load 'rubychem/valence.rb'
2
+ load 'rubychem/valence.rb'
3
+ load 'rubychem/equation.rb'
@@ -1,14 +1,14 @@
1
1
  module RubyChem
2
2
  class Compound
3
3
 
4
- MASSES = { :H => 1.01, :He => 4.00, :Li => 6.94, :Be => 9.01, :B => 10.81, :C => 12.01, :N => 14.01, :F => 19.00, :Ne => 20.18, :S => 32.01, :O => 15.99,
5
- :Na => 22.99, :Mg => 24.31, :Al => 26.98, :Si => 28.09, :P => 30.97, :Cl => 35.45, :Ar => 39.95, :K => 39.1, :Ca => 40.08, :Sc => 44.96, :Ti => 47.88, :V => 50.94,
6
- :Cr => 52.00, :Mn => 54.94, :Fe => 55.85, :Co => 58.93, :Ni => 58.69, :Cu => 63.55, :Zn => 65.39, :Ga => 69.72, :Ge=> 72.61, :As => 74.92, :Se => 78.96,
7
- :Br => 79.90, :Kr => 83.80, :Rb => 85.47, :Sr => 87.62, :Y => 88.91, :Zr => 91.22, :Nb => 92.91, :Mo => 95.94, :Te => 98, :Ru => 101.07, :Rh => 102.91,
8
- :Pd => 106.42, :Ag => 107.87, :Cd => 112.41, :In => 114.82, :Sn => 118.71, :Sb => 121.76, :Te => 127.6, :I => 126.9, :Xe => 131.29, :Cs => 132.9, :Ba => 137.3,
9
- :La => 138.9, :Hf => 178.5, :Ta => 180.9, :W => 183.9, :Re => 186.2, :Os => 190.2, :Ir => 192.2, :Pt => 195.1, :Au => 197.0, :Hg => 200.6, :Ti => 204.4,
10
- :Pb => 207.2, :Bi => 209, :Po => 209, :At => 210, :Rn => 222, :Fr => 223, :Ra => 226, :Ac => 227, :Rf => 261, :Db => 262, :Sg => 263, :Bh => 264, :Hs => 265,
11
- :Mt => 268, :Ds => 271, :Rg => 272 }
4
+ MASSES = { H: 1.01, He: 4.00, Li: 6.94, Be: 9.01, B: 10.81, C: 12.01, N: 14.01, F: 19.00, Ne: 20.18, S: 32.01, O: 15.99,
5
+ Na: 22.99, Mg: 24.31, Al: 26.98, Si: 28.09, P: 30.97, Cl: 35.45, Ar: 39.95, K: 39.1, Ca: 40.08, Sc: 44.96, Ti: 47.88, V: 50.94,
6
+ Cr: 52.00, Mn: 54.94, Fe: 55.85, Co: 58.93, Ni: 58.69, Cu: 63.55, Zn: 65.39, Ga: 69.72, Ge: 72.61, As: 74.92, Se: 78.96,
7
+ Br: 79.90, Kr: 83.80, Rb: 85.47, Sr: 87.62, Y: 88.91, Zr: 91.22, Nb: 92.91, Mo: 95.94, Te: 98, Ru: 101.07, Rh: 102.91,
8
+ Pd: 106.42, Ag: 107.87, Cd: 112.41, In: 114.82, Sn: 118.71, Sb: 121.76, Te: 127.6, I: 126.9, Xe: 131.29, Cs: 132.9, Ba: 137.3,
9
+ La: 138.9, Hf: 178.5, Ta: 180.9, W: 183.9, Re: 186.2, Os: 190.2, Ir: 192.2, Pt: 195.1, Au: 197.0, Hg: 200.6, Ti: 204.4,
10
+ Pb: 207.2, Bi: 209, Po: 209, At: 210, Rn: 222, Fr: 223, Ra: 226, Ac: 227, Rf: 261, Db: 262, Sg: 263, Bh: 264, Hs: 265,
11
+ Mt: 268, Ds: 271, Rg: 272 }
12
12
 
13
13
  attr_accessor :chem_species, :mm, :fw
14
14
 
@@ -67,32 +67,5 @@ module RubyChem
67
67
  end
68
68
  end
69
69
 
70
- class BalanceChem
71
- attr_accessor :balanced, :lefteq, :righteq, :leftfw, :rightfw, :count
72
-
73
- ##
74
- # Checks if two formulas are balanced.
75
- #
76
- def initialize(lefteq, righteq)
77
- @lefteq = lefteq
78
- @righteq = righteq
79
- @leftfw = RubyChem::Chemical.new(@lefteq)
80
- @rightfw = RubyChem::Chemical.new(@righteq)
81
- @balanced = ( RubyChem::Chemical.new(@lefteq).fw - RubyChem::Chemical.new(@righteq).fw ).abs < 0.00001
82
- end
83
-
84
- def balanceit
85
- @count = 0
86
- @leftfw = @leftfw.fw
87
- @rightfw = @rightfw.fw
88
- until @balanced == true
89
- @rightfw = @rightfw * 2
90
- @balanced = (@leftfw - @rightfw).abs < 0.00001
91
- @count += 1
92
- end
93
- puts "#{@lefteq} (#{@leftfw}) -> #{@righteq} (#{@rightfw}) is a balanced equation; #{@count} iterations"
94
- end
95
-
96
-
97
- end
70
+
98
71
  end
@@ -0,0 +1,35 @@
1
+ module RubyChem
2
+ class Equation
3
+ attr_accessor :left, :right
4
+
5
+ # Checks if two formulas are balanced.
6
+ # Parse string... so that x + y + z = a + b
7
+ # yields @left[Chemical1, Chemical2, Chemical3], @right[Chemical1,Chemical2]
8
+
9
+ def initialize(equation)
10
+ @left = Array.new
11
+ @right = Array.new
12
+ left_and_right = equation.split(/\=/)
13
+ process_sides(left_and_right)
14
+ end
15
+
16
+ def process_sides(left_and_right)
17
+ process(left_and_right[0], "left")
18
+ process(left_and_right[1], "right")
19
+ end
20
+
21
+ def process(equation, side)
22
+ if side == "left"
23
+ equation.split(/\+/).each do |chemical|
24
+ @left << RubyChem::Chemical.new(chemical.strip)
25
+ end
26
+ else
27
+ equation.split(/\+/).each do |chemical|
28
+ @right << RubyChem::Chemical.new(chemical.strip)
29
+ end
30
+ end
31
+ end
32
+
33
+
34
+ end
35
+ end
@@ -1,42 +1,40 @@
1
1
  module RubyChem
2
- class Lewis
3
- attr_accessor :structure
4
- Valence = {"O" => 6, "C" => 4}
5
- Species = [["C"], ["O", "2"]]
2
+ class Lewis
3
+ attr_accessor :structure
4
+ Valence = {"O" => 6, "C" => 4}
5
+ Species = [["C"], ["O", "2"]]
6
6
 
7
- def initialize
8
- @species = Species
9
- determine_configuration
10
- end
7
+ def initialize
8
+ @species = Species
9
+ determine_configuration
10
+ end
11
11
 
12
- private
13
-
14
- def determine_configuration
15
- @valence_electrons_available = valence_electrons_available(@species)
16
- @valence_electrons_needed = valence_electrons_needed_to_complete_shell(@species) * 8
17
- @shared_electrons = @valence_electrons_needed - @valence_electrons_available
18
- end
12
+ private
13
+
14
+ def determine_configuration
15
+ @valence_electrons_available = valence_electrons_available(@species)
16
+ @valence_electrons_needed = valence_electrons_needed_to_complete_shell(@species) * 8
17
+ @shared_electrons = @valence_electrons_needed - @valence_electrons_available
18
+ end
19
19
 
20
- def valence_electrons_available(species)
21
- electrons = []
22
- species.each do |x|
23
- if x.count > 1
24
- x.count.times do
25
- electrons << Valence[x.first]
26
- end
27
- else
28
- electrons << Valence[x.first]
29
- end
20
+ def valence_electrons_available(species)
21
+ electrons = []
22
+ species.each do |x|
23
+ if x.count > 1
24
+ x.count.times do
25
+ electrons << Valence[x.first]
26
+ end
27
+ else
28
+ electrons << Valence[x.first]
29
+ end
30
+ end
31
+ electrons.inject{|sum,x| sum + x }
30
32
  end
31
- electrons.inject{|sum,x| sum + x }
32
- end
33
33
 
34
- def valence_electrons_needed_to_complete_shell(species)
35
- array = []
36
- species.each{|x|array << x.count}
37
- array.inject{|sum,x| sum + x}
34
+ def valence_electrons_needed_to_complete_shell(species)
35
+ array = []
36
+ species.each{|x|array << x.count}
37
+ array.inject{|sum,x| sum + x}
38
+ end
38
39
  end
39
-
40
-
41
- end
42
- end
40
+ end
@@ -24,39 +24,13 @@ module RubyChem
24
24
 
25
25
  private
26
26
 
27
- def speciate(x,grams=1)
28
- @chem_species = x.map { |chem| chem.scan(/[A-Z][^A-Z]*/) }.flatten
29
- @chem_species.map! {|chem| chem.scan /[A-Z]+|\d+/i }
30
- atom_masses = @chem_species.map { |(elem, coeff)| MASSES[elem.to_sym] * (coeff || 1).to_f }
31
- x = atom_masses.map { |int| int.to_f }
32
- @mm = x.inject(0) { |s,v| s+= v }
33
- @moles = grams/@mm
34
- end
35
- end
36
-
37
- class BalanceChem
38
- attr_accessor :balanced, :lefteq, :righteq, :leftfw, :rightfw, :count
39
-
40
- # Checks if two formulas are balanced.
41
- #
42
- def initialize(lefteq, righteq)
43
- @lefteq = lefteq
44
- @righteq = righteq
45
- @balanced = ( RubyChem::Chemical.new(@lefteq).mm - RubyChem::Chemical.new(@righteq).mm ).abs < 0.00001
46
- end
47
-
48
- def balanceit
49
- @count = 0
50
- @leftfw = @leftfw.fw
51
- @rightfw = @rightfw.fw
52
- until @balanced == true
53
- @rightfw = @rightfw * 2
54
- @balanced = (@leftfw - @rightfw).abs < 0.00001
55
- @count += 1
56
- end
57
- puts "#{@lefteq} (#{@leftfw}) -> #{@righteq} (#{@rightfw}) is a balanced equation; #{@count} iterations"
27
+ def speciate(x,grams=1)
28
+ @chem_species = x.map { |chem| chem.scan(/[A-Z][^A-Z]*/) }.flatten
29
+ @chem_species.map! {|chem| chem.scan /[A-Z]+|\d+/i }
30
+ atom_masses = @chem_species.map { |(elem, coeff)| MASSES[elem.to_sym] * (coeff || 1).to_f }
31
+ x = atom_masses.map { |int| int.to_f }
32
+ @mm = x.inject(0) { |s,v| s+= v }
33
+ @moles = grams/@mm
34
+ end
58
35
  end
59
-
60
-
61
- end
62
- end
36
+ end
@@ -1,3 +1,3 @@
1
1
  module Rubychem
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubychem
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-05 00:00:00.000000000 Z
12
+ date: 2013-02-12 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: This is an open source library of chemistry, goodies. I'm dedicating
15
15
  this code to my major professor Arthur Brecher and Graduate PI Dr. Rogers of Bowling
@@ -34,13 +34,12 @@ files:
34
34
  - lib/benchmark.rb
35
35
  - lib/rubychem.rb
36
36
  - lib/rubychem/comments.rb
37
+ - lib/rubychem/equation.rb
37
38
  - lib/rubychem/lewis.rb
38
39
  - lib/rubychem/molecule.rb
39
40
  - lib/rubychem/valence.rb
40
41
  - lib/rubychem/version.rb
41
42
  - lib/rubychem_rspec.rb
42
- - rubychem-1.0.1.gem
43
- - rubychem-1.0.2.gem
44
43
  - rubychem.gemspec
45
44
  - spec/rubychem/ruby_chem_spec.rb
46
45
  - spec/spec_helper.rb
data/rubychem-1.0.1.gem DELETED
Binary file
data/rubychem-1.0.2.gem DELETED
Binary file