class ChemistryParadise::SplitMoleculeNames

Public Class Methods

new( i = 'C12H11O11' ) click to toggle source
#

initialize

#
# 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

Public Instance Methods

determine_total(i = @result) click to toggle source
#

determine_total

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

#
# File lib/chemistry_paradise/split_molecule_names.rb, line 117
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) click to toggle source
#

ignore_numbers

This will ignore numbers in a guaranteed manner.

#
# File lib/chemistry_paradise/split_molecule_names.rb, line 92
def ignore_numbers(i)
  return i.gsub(/\d+/,'')
end
is_number?(i) click to toggle source
#

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.

#
# File lib/chemistry_paradise/split_molecule_names.rb, line 53
def is_number?(i)
  result = (i.to_i.to_s == i)
  return result
end
reset() click to toggle source
#

reset

#
Calls superclass method ChemistryParadise::Base#reset
# File lib/chemistry_paradise/split_molecule_names.rb, line 34
def reset
  super()
  @result = []
  @total  = Hash.new(0) # Hash that keeps track of n elements each. Default value will be 0.
end
result()
Alias for: result?
result?() click to toggle source
#

result?

#
# File lib/chemistry_paradise/split_molecule_names.rb, line 43
def result?
  @result
end
Also aliased as: result
return_n_elements(i) click to toggle source
#

return_n_elements

Must return an Integer.

#
# File lib/chemistry_paradise/split_molecule_names.rb, line 101
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) click to toggle source
#

set_input

#
# File lib/chemistry_paradise/split_molecule_names.rb, line 138
def set_input(i)
  if i
    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 = i.gsub(/(\d)+ /,'\1')
        i = i.gsub(/ \+ /,'')
      end
      # ===================================================================== #
      # But it can also include internal ' ', which we will remove next.
      # ===================================================================== #
      i.delete!(' ')
    end
  end
  @input = i
end
split(i = @input) click to toggle source
#

split

This method will return an array with all the elements.

#
# File lib/chemistry_paradise/split_molecule_names.rb, line 63
def split(i = @input)
  array = []
  _ = ''.dup
  if i
    i.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?() click to toggle source
#

total?

#
# File lib/chemistry_paradise/split_molecule_names.rb, line 131
def total?
  @total # This must always be a Hash.
end