rubychem 1.0.0 → 1.0.1
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/Gemfile.lock +1 -1
- data/README.markdown +2 -7
- data/features/step_definitions/user_starts_program_steps.rb +26 -0
- data/features/step_definitions/user_submits_compound_steps.rb +12 -0
- data/features/support/env.rb +2 -0
- data/features/user_starts_program.feature +10 -0
- data/features/user_submits_compound.feature +19 -0
- data/lib/benchmark.rb +9 -6
- data/lib/{oldrubychem.rb → comments.rb} +2 -2
- data/lib/rubychem/version.rb +1 -1
- data/lib/rubychem_rspec.rb +51 -0
- data/lib/valence.rb +73 -0
- data/rubychem.gemspec +1 -1
- data/spec/rubychem/ruby_chem_spec.rb +30 -0
- data/spec/spec_helper.rb +1 -0
- metadata +41 -42
data/Gemfile.lock
CHANGED
data/README.markdown
CHANGED
@@ -9,10 +9,5 @@ Example:
|
|
9
9
|
|
10
10
|
# Installation
|
11
11
|
|
12
|
-
*
|
13
|
-
|
14
|
-
* git://github.com/fogonthedowns/rubychem.git
|
15
|
-
* ruby script/generate scaffold chem formula:string
|
16
|
-
* rake db:migrate
|
17
|
-
* Add "include RubyChem" in the application_controller.rb
|
18
|
-
* in views/chems/show.html.erb add the following code: Molecular Mass: <%= RubyChem::Chemical.new(@chem.formula.to_s).fw %>
|
12
|
+
* gem install rubychem
|
13
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
Given /^I am not yet using the program$/ do
|
2
|
+
end
|
3
|
+
|
4
|
+
When /^I start the program$/ do
|
5
|
+
chemical = Mass::Compound.new(output)
|
6
|
+
chemical.start('H2O')
|
7
|
+
end
|
8
|
+
|
9
|
+
Then /^I should see "([^"]*)"$/ do |message|
|
10
|
+
output.messages.should include(message)
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
class Output
|
15
|
+
def messages
|
16
|
+
@messages ||= []
|
17
|
+
end
|
18
|
+
|
19
|
+
def puts(message)
|
20
|
+
messages << message
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def output
|
25
|
+
@output ||= Output.new
|
26
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
Feature: user starts program
|
2
|
+
As a user
|
3
|
+
I want to start the program
|
4
|
+
So that I can find the molecular mass
|
5
|
+
|
6
|
+
Scenario: start game
|
7
|
+
Given I am not yet using the program
|
8
|
+
When I start the program
|
9
|
+
Then I should see "Welcome to RubyChem!"
|
10
|
+
And I should see "Enter a Compound:"
|
@@ -0,0 +1,19 @@
|
|
1
|
+
Feature: user submits compound
|
2
|
+
|
3
|
+
The user submits a chemical equation. The program returns the molecular mass
|
4
|
+
|
5
|
+
For the chemical compound the user submits, the program iterates through the equation to return
|
6
|
+
the number of atoms. The program multiplies the number of atoms times each specific molecular mass
|
7
|
+
and sums the masses. The program returns the total molecular mass
|
8
|
+
|
9
|
+
Scenario Outline: submit compound
|
10
|
+
When I submit "<compound>"
|
11
|
+
Then the molecular mass should be "<mass>"
|
12
|
+
|
13
|
+
Scenarios: no matches
|
14
|
+
| compound | mass |
|
15
|
+
| H2O | 18.01 |
|
16
|
+
| O | 15.99 |
|
17
|
+
| H2SO4 | 97.99 |
|
18
|
+
| NaCl | 58.44 |
|
19
|
+
| C6H12O6 | 180.12 |
|
data/lib/benchmark.rb
CHANGED
@@ -1,32 +1,35 @@
|
|
1
1
|
# The goal is for beta.rb to run faster
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
require 'rubygems'
|
4
|
+
require 'benchmark'
|
5
|
+
|
5
6
|
load 'rubychem.rb'
|
7
|
+
load 'comments.rb'
|
8
|
+
|
6
9
|
|
7
10
|
iterations = 10000
|
8
11
|
|
9
12
|
a = Benchmark.measure do
|
10
13
|
for i in 1..iterations do
|
11
|
-
RubyChem::Chemical.new("H2O")
|
14
|
+
RubyChem::Chemical.new("H2O")
|
12
15
|
end
|
13
16
|
end
|
14
17
|
|
15
18
|
b = Benchmark.measure do
|
16
19
|
for i in 1..iterations do
|
17
|
-
|
20
|
+
RubyChem::Compound.new("H2O").fw
|
18
21
|
end
|
19
22
|
end
|
20
23
|
|
21
24
|
c = Benchmark.measure do
|
22
25
|
for i in 1..iterations do
|
23
|
-
RubyChem::Chemical.new("H2SO4")
|
26
|
+
RubyChem::Chemical.new("H2SO4")
|
24
27
|
end
|
25
28
|
end
|
26
29
|
|
27
30
|
d = Benchmark.measure do
|
28
31
|
for i in 1..iterations do
|
29
|
-
|
32
|
+
RubyChem::Compound.new("H2SO4").fw
|
30
33
|
end
|
31
34
|
end
|
32
35
|
|
@@ -1,5 +1,5 @@
|
|
1
|
-
module
|
2
|
-
class
|
1
|
+
module RubyChem
|
2
|
+
class Compound
|
3
3
|
|
4
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
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,
|
data/lib/rubychem/version.rb
CHANGED
@@ -0,0 +1,51 @@
|
|
1
|
+
module Mass
|
2
|
+
class Compound
|
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 }
|
12
|
+
|
13
|
+
attr_accessor :chem_species, :mm
|
14
|
+
|
15
|
+
def initialize(output)
|
16
|
+
@output = output
|
17
|
+
end
|
18
|
+
|
19
|
+
def start
|
20
|
+
@output.puts 'Welcome to RubyChem!'
|
21
|
+
@output.puts 'Enter a Compound:'
|
22
|
+
end
|
23
|
+
|
24
|
+
def fw(formula)
|
25
|
+
if formula.scan(/\d+$/) == []
|
26
|
+
x = formula.gsub(/$/, '1').scan(/[A-za-z]*\d+/)
|
27
|
+
speciate(x)
|
28
|
+
else
|
29
|
+
x = formula.scan(/[A-za-z]*\d+/)
|
30
|
+
speciate(x)
|
31
|
+
end
|
32
|
+
@output.puts @mm.round_to(4).to_s
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def speciate(x)
|
38
|
+
@chem_species = x.map { |chem| chem.scan(/[A-Z][^A-Z]*/) }.flatten
|
39
|
+
@chem_species.map! {|chem| chem.scan /[A-Z]+|\d+/i }
|
40
|
+
atom_masses = @chem_species.map { |(elem, coeff)| MASSES[elem.to_sym] * (coeff || 1).to_f }
|
41
|
+
x = atom_masses.map { |int| int.to_f }
|
42
|
+
@mm = x.inject(0) { |s,v| s+= v }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
class Float
|
48
|
+
def round_to(x)
|
49
|
+
(self * 10**x).round.to_f / 10**x
|
50
|
+
end
|
51
|
+
end
|
data/lib/valence.rb
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
module RubyChem
|
2
|
+
class Valence
|
3
|
+
Fillorder = ["1s","2s","2p","3s","3p","4s","3d","4p","5s","4d","5p","6s","4f","5d","6p","7s","5f","6d","7p"]
|
4
|
+
attr_accessor :calc_valence
|
5
|
+
|
6
|
+
def initialize(k)
|
7
|
+
@mass = k
|
8
|
+
determine_valence(@mass)
|
9
|
+
@calc_valence = Hash.new
|
10
|
+
@calc_valence = calculate_electronic_config
|
11
|
+
identify_valence_electrons
|
12
|
+
end
|
13
|
+
|
14
|
+
|
15
|
+
def calculate_electronic_config
|
16
|
+
@shell_location = 0
|
17
|
+
1.upto(100) do |x|
|
18
|
+
until @v <= 0
|
19
|
+
shell_electrons = determine_valence_electrons_in_shell(@shell_location)
|
20
|
+
@before_subtract = @v
|
21
|
+
@v -= shell_electrons
|
22
|
+
@calc_valence[Fillorder[@shell_location]] = shell_electrons
|
23
|
+
move_on(@v)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
@calc_valence
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def determine_valence(mass)
|
32
|
+
@v = (mass / 1).round.to_i
|
33
|
+
end
|
34
|
+
|
35
|
+
def determine_valence_electrons_in_shell(x)
|
36
|
+
y = Fillorder[x].match(/\D/)[0]
|
37
|
+
if y == "s"
|
38
|
+
2
|
39
|
+
elsif y == "p"
|
40
|
+
6
|
41
|
+
elsif y == "d"
|
42
|
+
10
|
43
|
+
elsif y == "f"
|
44
|
+
14
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def move_on(v)
|
49
|
+
if v > 0
|
50
|
+
@shell_location += 1
|
51
|
+
else
|
52
|
+
@calc_valence.delete(@shell_location)
|
53
|
+
@calc_valence[Fillorder[@shell_location]] = @before_subtract
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def identify_valence_electrons
|
58
|
+
@valence_electrons = Hash.new
|
59
|
+
high_number = 0
|
60
|
+
@calc_valence.each_key do |x|
|
61
|
+
high_number = x.match(/\d/)[0].to_i if x.match(/\d/)[0].to_i > high_number
|
62
|
+
end
|
63
|
+
|
64
|
+
@calc_valence.each do |k,x|
|
65
|
+
if k.match(/\d/)[0].to_i == high_number
|
66
|
+
@valence_electrons[k] = x
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
73
|
+
end
|
data/rubychem.gemspec
CHANGED
@@ -10,7 +10,7 @@ Gem::Specification.new do |s|
|
|
10
10
|
s.email = ["justin.zollars@gmail.com"]
|
11
11
|
s.homepage = "http://www.justinzollars.com"
|
12
12
|
s.summary = %q{A ruby chemistry library}
|
13
|
-
s.description = %q{This is an open source library of chemistry, goodies. I'm dedicating this code to my major professor Arthur Brecher and Graduate PI Dr. Rogers of Bowling Green State University
|
13
|
+
s.description = %q{This is an open source library of chemistry, goodies. I'm dedicating this code to my major professor Arthur Brecher and Graduate PI Dr. Rogers of Bowling Green State University}
|
14
14
|
|
15
15
|
s.rubyforge_project = "rubychem"
|
16
16
|
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Mass
|
4
|
+
describe Compound do
|
5
|
+
let(:output) {double('output').as_null_object}
|
6
|
+
let(:chemical) {Compound.new(output)}
|
7
|
+
|
8
|
+
describe "#start" do
|
9
|
+
it "sends a welcome message" do
|
10
|
+
output.should_receive(:puts).with('Welcome to RubyChem!')
|
11
|
+
chemical.start
|
12
|
+
end
|
13
|
+
|
14
|
+
it "prompts for the compound" do
|
15
|
+
output.should_receive(:puts).with('Enter a Compound:')
|
16
|
+
chemical.start
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#fw" do
|
21
|
+
context "H2O" do
|
22
|
+
it "returns 18.01" do
|
23
|
+
chemical.start
|
24
|
+
output.should_receive(:puts).with('18.01')
|
25
|
+
chemical.fw('H2O')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'ruby_chem'
|
metadata
CHANGED
@@ -1,33 +1,25 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: rubychem
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
|
6
|
-
- 1
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
version: 1.0.0
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.1
|
5
|
+
prerelease:
|
10
6
|
platform: ruby
|
11
|
-
authors:
|
7
|
+
authors:
|
12
8
|
- Justin Zollars
|
13
9
|
autorequire:
|
14
10
|
bindir: bin
|
15
11
|
cert_chain: []
|
16
|
-
|
17
|
-
date: 2011-11-06 01:00:00 -07:00
|
18
|
-
default_executable:
|
12
|
+
date: 2011-11-14 00:00:00.000000000Z
|
19
13
|
dependencies: []
|
20
|
-
|
21
|
-
|
22
|
-
|
14
|
+
description: This is an open source library of chemistry, goodies. I'm dedicating
|
15
|
+
this code to my major professor Arthur Brecher and Graduate PI Dr. Rogers of Bowling
|
16
|
+
Green State University
|
17
|
+
email:
|
23
18
|
- justin.zollars@gmail.com
|
24
19
|
executables: []
|
25
|
-
|
26
20
|
extensions: []
|
27
|
-
|
28
21
|
extra_rdoc_files: []
|
29
|
-
|
30
|
-
files:
|
22
|
+
files:
|
31
23
|
- .DS_Store
|
32
24
|
- .gitignore
|
33
25
|
- Gemfile
|
@@ -35,43 +27,50 @@ files:
|
|
35
27
|
- LICENSE
|
36
28
|
- README.markdown
|
37
29
|
- Rakefile
|
30
|
+
- features/step_definitions/user_starts_program_steps.rb
|
31
|
+
- features/step_definitions/user_submits_compound_steps.rb
|
32
|
+
- features/support/env.rb
|
33
|
+
- features/user_starts_program.feature
|
34
|
+
- features/user_submits_compound.feature
|
38
35
|
- lib/.DS_Store
|
39
36
|
- lib/benchmark.rb
|
40
|
-
- lib/
|
37
|
+
- lib/comments.rb
|
41
38
|
- lib/rubychem.rb
|
42
39
|
- lib/rubychem/version.rb
|
40
|
+
- lib/rubychem_rspec.rb
|
41
|
+
- lib/valence.rb
|
43
42
|
- rubychem.gemspec
|
44
|
-
|
43
|
+
- spec/rubychem/ruby_chem_spec.rb
|
44
|
+
- spec/spec_helper.rb
|
45
45
|
homepage: http://www.justinzollars.com
|
46
46
|
licenses: []
|
47
|
-
|
48
47
|
post_install_message:
|
49
48
|
rdoc_options: []
|
50
|
-
|
51
|
-
require_paths:
|
49
|
+
require_paths:
|
52
50
|
- lib
|
53
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
52
|
none: false
|
55
|
-
requirements:
|
56
|
-
- -
|
57
|
-
- !ruby/object:Gem::Version
|
58
|
-
|
59
|
-
|
60
|
-
version: "0"
|
61
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
62
58
|
none: false
|
63
|
-
requirements:
|
64
|
-
- -
|
65
|
-
- !ruby/object:Gem::Version
|
66
|
-
|
67
|
-
- 0
|
68
|
-
version: "0"
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
69
63
|
requirements: []
|
70
|
-
|
71
64
|
rubyforge_project: rubychem
|
72
|
-
rubygems_version: 1.
|
65
|
+
rubygems_version: 1.8.10
|
73
66
|
signing_key:
|
74
67
|
specification_version: 3
|
75
68
|
summary: A ruby chemistry library
|
76
|
-
test_files:
|
77
|
-
|
69
|
+
test_files:
|
70
|
+
- features/step_definitions/user_starts_program_steps.rb
|
71
|
+
- features/step_definitions/user_submits_compound_steps.rb
|
72
|
+
- features/support/env.rb
|
73
|
+
- features/user_starts_program.feature
|
74
|
+
- features/user_submits_compound.feature
|
75
|
+
- spec/rubychem/ruby_chem_spec.rb
|
76
|
+
- spec/spec_helper.rb
|