class ChemistryParadise::InteractiveChemistryShell

Constants

PERIODIC_TABLE
PROMPT
#

PROMPT

#

Public Class Methods

new(run_already = true) click to toggle source
#

initialize

#
# File lib/chemistry_paradise/interactive_chemistry_shell.rb, line 34
def initialize(run_already = true)
  reset
  run if run_already
end

Public Instance Methods

calc(i)
Alias for: calculate
calculate(i) click to toggle source
#

calculate

To test this, do:

calc C3H5
#
# File lib/chemistry_paradise/interactive_chemistry_shell.rb, line 77
def calculate(i)
  if i.nil?
    e 'Please provide proper input to this method now, for instance, `C12H12N2`:'
    i = $stdin.gets.chomp
  end
  CalculateAtomicMass.new(i)
end
Also aliased as: calc
check_against_menu() click to toggle source
#

check_against_menu

#
# File lib/chemistry_paradise/interactive_chemistry_shell.rb, line 88
def check_against_menu
  _ = @user_input # We can use this now.
  arguments = nil # Default to denote that we have no arguments.
  if _.include? ' '
    splitted = _.split(' ')
    _ = splitted[0] # Reassign here to the first entry.
    arguments = splitted[1..-1]
  end
  case _ # case tag
  # ======================================================================= #
  # === quit
  # ======================================================================= #
  when 'quit','exit','q'
    e 'Bye from the InteractiveChemistryShell.'
    exit
  # ======================================================================= #
  # === show_electron_negativity_chart
  # ======================================================================= #
  when '4','show_electron_negativity_chart'
    ChemistryParadise.show_electron_negativity_chart
  # ======================================================================= #
  # === periodic_table?
  # ======================================================================= #
  when '3','periodic_table?','show_periodic_table','top','top?',
       'periodic?','periodic','showperiodictable'
    show_periodic_table
  # ======================================================================= #
  # === help
  # ======================================================================= #
  when /-?-?help/,'hel','he','h','?'
    show_help
  when '1','calc','calculate'
    calculate(arguments)
  when '2','show'
    e 'Input your element symbol now:'
    element_symbol = $stdin.gets.chomp
    ShowElectronConfiguration.new(element_symbol)
  when '' # pass through
  else
    # To test the following, try:
    #   454 g NH4NO3
    if @user_input.include?('g') and @user_input =~ /\d+/
      splitted = @user_input.strip.split('g').map(&:strip)
      atomic_mass = ChemistryParadise::CalculateAtomicMass[splitted.last].to_f
      n_times = splitted.first.strip.to_i
      mass = n_times / atomic_mass
      e (mass.round(2)).to_s+' mol'
    else
      report_input(_)
      show_help
    end
  end
end
colourize_via_kde_konsole(i) click to toggle source
#

colourize_via_kde_konsole

#
# File lib/chemistry_paradise/interactive_chemistry_shell.rb, line 230
def colourize_via_kde_konsole(i)
  if Object.const_defined? :Colours
    i = ::Colours.lightgreen(i)
  end
  i
end
ecomment(i) click to toggle source
#

ecomment

#
# File lib/chemistry_paradise/interactive_chemistry_shell.rb, line 145
def ecomment(i)
  Colours.ecomment(i)
end
fetch_user_input() click to toggle source
#

fetch_user_input

#
# File lib/chemistry_paradise/interactive_chemistry_shell.rb, line 198
def fetch_user_input
  if has_readline?
    @user_input = Readline.readline('', true)
  else
    @user_input = $stdin.gets.chomp
  end
end
has_readline?() click to toggle source
#

has_readline?

#
# File lib/chemistry_paradise/interactive_chemistry_shell.rb, line 191
def has_readline?
  Object.const_defined? :Readline
end
report_input(i) click to toggle source
#

report_input

#
# File lib/chemistry_paradise/interactive_chemistry_shell.rb, line 59
def report_input(i)
  ewarn 'Input was: `'+sfancy(i)+'`'
end
reset() click to toggle source
#

reset

#
# File lib/chemistry_paradise/interactive_chemistry_shell.rb, line 42
def reset
  @lpad = '  '
end
run() click to toggle source
#

run (run tag)

#
# File lib/chemistry_paradise/interactive_chemistry_shell.rb, line 162
def run
  show_welcome_message_and_help # Show the help section on startup.
  run_loop
end
run_loop() click to toggle source
#

run_loop

#
# File lib/chemistry_paradise/interactive_chemistry_shell.rb, line 152
def run_loop
  loop {
    fetch_user_input # Get user input first.
    check_against_menu
  }
end
show_help() click to toggle source
#

show_help

#
# File lib/chemistry_paradise/interactive_chemistry_shell.rb, line 170
def show_help # help tag
  e 'Available options:'
  e
  # ======================================================================= #
  # Add more documented help-options to this Array here.
  # ======================================================================= #
  array = [
    'calculate the atomic mass',
    'show the electron configuration of an Element',
    'show the periodic table',
    'show electron negativity chart'
  ]
  array.each_with_index {|entry, index|
    index += 1
    ecomment @lpad+index.to_s+' # '+entry
  }
end
show_periodic_table() click to toggle source
#

show_periodic_table

#
# File lib/chemistry_paradise/interactive_chemistry_shell.rb, line 209
def show_periodic_table
  e # Add a newline.
  inverted = PERIODIC_TABLE.invert
  1.upto(7).each {|periode|
    range = return_range_for_this_period(periode)
    # ===================================================================== #
    # Now we have a proper range.
    # ===================================================================== #
    matches = inverted.select {|index_position, value|
      range.include? index_position
    }
    ee colourize_via_kde_konsole(periode.to_s)+': ' # Display the periode here.
    matches.each_pair {|key, value|
      ee simp(value)+' ('+key.to_s+') '
    }; e
  }
end
show_welcome_message() click to toggle source
#

show_welcome_message

#
# File lib/chemistry_paradise/interactive_chemistry_shell.rb, line 49
def show_welcome_message
  e 'Welcome to the InteractiveChemistryShell.'
  e 'Input "help" to get a list of options, otherwise'
  e 'just type in your input now.'
  print PROMPT
end
show_welcome_message_and_help() click to toggle source
#

show_welcome_message_and_help

#
# File lib/chemistry_paradise/interactive_chemistry_shell.rb, line 66
def show_welcome_message_and_help
  show_welcome_message
  show_help
end