Class: ChemistryParadise::SplitMoleculeNames

Inherits:
Base
  • Object
show all
Defined in:
lib/chemistry_paradise/split_molecule_names.rb

Constant Summary

Constants inherited from Base

Base::FILE_MOLECULAR_FORMULA_OF_DIFFERENT_MOLECULES, Base::NAMESPACE

Constants included from Shared

ChemistryParadise::Shared::ARRAY_TEST_THESE_MOLECULES

Constants included from Constants

Constants::ELECTRON_NEGATIVITY_CHART, Constants::FILE_ATOMGEWICHTE, Constants::FILE_ELECTRON_NEGATIVITY_CHART, Constants::FILE_PERIODIC_TABLE_OF_THE_ELEMENTS, Constants::N, Constants::PLANK_CONSTANT, Constants::PROPER_FILLORDER, Constants::SPEED_OF_LIGHT

Instance Method Summary collapse

Methods inherited from Base

#be_quiet, #be_verbose?, #cd, #cliner, #commandline_arguments?, #do_use_the_english_language, #do_use_the_german_language, #do_we_use_english?, #esystem, #first_argument?, #gold, #grey, #initialize_the_internal_hash, #internal_hash?, #is_on_roebe?, #mediumpurple, #namespace?, #olivedrab, #opnn, #rev, #royalblue, #set_be_verbose, #set_commandline_arguments, #sfancy, #steelblue, #teal, #tomato, #use_which_language?, #yellow

Methods included from Shared

#convert_parens, #periodic_table?, periodic_table?, #return_range_for_this_period, #square

Methods included from Constants

#electron_negativity_chart?

Constructor Details

#initialize(i = 'C12H11O11') ⇒ SplitMoleculeNames

#

initialize

#


23
24
25
26
27
28
29
# File 'lib/chemistry_paradise/split_molecule_names.rb', line 23

def initialize(
    i = 'C12H11O11'
  )
  reset
  set_input(i)
  split # This is the actual run method.
end

Instance Method Details

#determine_total(i = @result) ⇒ Object

#

determine_total

If the input is @result then it was already properly pre-sorted for us.

#


127
128
129
130
131
132
133
134
135
136
# File 'lib/chemistry_paradise/split_molecule_names.rb', line 127

def determine_total(i = @result)
  i.each {|entry|
    this_real_key = ignore_numbers(entry)
    if @total.has_key? this_real_key
      @total[this_real_key] = @total[this_real_key]+return_n_elements(entry)
    else # Else we must make a new key; the Laufindex kann aber 1-n sein.
      @total[this_real_key] = return_n_elements(entry)
    end
  }
end

#ignore_numbers(i) ⇒ Object

#

ignore_numbers

This will ignore numbers in a guaranteed manner.

#


102
103
104
# File 'lib/chemistry_paradise/split_molecule_names.rb', line 102

def ignore_numbers(i)
  return i.gsub(/\d+/,'')
end

#is_number?(i) ⇒ Boolean

#

is_number?

This returns true if the input is a number, else false. For this it uses the .to_i method trick, which returns 0 for non-numbers.

#

Returns:

  • (Boolean)


59
60
61
62
# File 'lib/chemistry_paradise/split_molecule_names.rb', line 59

def is_number?(i)
  result = (i.to_i.to_s == i)
  return result
end

#resetObject

#

reset (reset tag)

#


34
35
36
37
38
39
40
41
42
43
44
# File 'lib/chemistry_paradise/split_molecule_names.rb', line 34

def reset
  super()
  # ======================================================================= #
  # === @result
  # ======================================================================= #
  @result = []
  # ======================================================================= #
  # === @total
  # ======================================================================= #
  @total  = Hash.new(0) # Hash that keeps track of n elements each. Default value will be 0.
end

#result?Boolean Also known as: result

#

result?

#

Returns:

  • (Boolean)


49
50
51
# File 'lib/chemistry_paradise/split_molecule_names.rb', line 49

def result?
  @result
end

#return_n_elements(i) ⇒ Object

#

return_n_elements

Must return an Integer.

#


111
112
113
114
115
116
117
118
119
120
# File 'lib/chemistry_paradise/split_molecule_names.rb', line 111

def return_n_elements(i)
  result = 0
  if i =~ /\d+/ # Ok has at least one number.
    i = i[/(\d+)/][0]
  else
    i = 1
  end
  result = i.to_i
  return result
end

#set_input(i) ⇒ Object

#

set_input

#


148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/chemistry_paradise/split_molecule_names.rb', line 148

def set_input(i)
  if i
    i = i.join(' ').strip if i.is_a? Array
    i = i.dup if i.frozen?
    if i.include?(' ') and (i =~ /^\d+/) # starts with a number.
      # Input is e. g. "5 H2O"
      splitted = i.split(' ')
      n_times = splitted.first.to_i
      new_result = ''.dup
      splitted.last.chars.each {|this_char|
        if this_char =~ /\d+/
          this_char = this_char.to_i * n_times
        end
        new_result << this_char.to_s
      }
      unless new_result[-1, 1] =~ /\d+/
        new_result << n_times.to_s
      end
      i = new_result
    end
    i.tr!('', '1') if i.include? ''
    i.tr!('', '2') if i.include? ''
    i.tr!('', '3') if i.include? ''
    i.tr!('', '4') if i.include? ''
    i.tr!('', '5') if i.include? ''
    i.tr!('', '6') if i.include? ''
    i.tr!('', '7') if i.include? ''
    i.tr!('', '8') if i.include? ''
    i.tr!('', '9') if i.include? ''
    i = convert_parens(i) if i.include? ')'
    if i.include? ' '
      i.strip!
      if i.include? '+' # Ok, input here can be like: '2 Fe + 3 Cl2'
        i.gsub!(/(\d)+ /,'\1')
        i.gsub!(/ \+ /,'')
      end
      # ===================================================================== #
      # But it can also include internal ' ', which we will remove next.
      # ===================================================================== #
      i.delete!(' ')
    end
  end
  @input = i
end

#split(i = @input) ⇒ Object

#

split

This method will return an array with all the elements.

#


69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/chemistry_paradise/split_molecule_names.rb', line 69

def split(
    i = @input
  )
  array = []
  _ = ''.dup
  i = i.first if i.is_a? Array
  if i
    chars = i.chars
    chars.each_with_index {|token, index|
      if is_number? token # We found a number here. We simply append it then.
        _ << token # return the old data
      elsif token.downcase == token
        _ << token
      else # Not a number, must be a character.
        unless _.empty?
          array << _
          _ = ''.dup
        end
        _ = token
      end
      array << _ if (index + 1) == i.size
    }
    @result = array
    determine_total
    return array
  end
end

#total?Boolean

#

total?

#

Returns:

  • (Boolean)


141
142
143
# File 'lib/chemistry_paradise/split_molecule_names.rb', line 141

def total?
  @total # This must always be a Hash.
end