class ChemistryParadise::ShowElement

Constants

NAMESPACE
#

NAMESPACE

#
PERIODIC_TABLE
#

PERIODIC_TABLE

#

Public Class Methods

new( i = nil, run_already = true ) click to toggle source
#

initialize

#
# File lib/chemistry_paradise/show_element.rb, line 33
def initialize(
    i           = nil,
    run_already = true
  )
  reset
  set_input(i)
  run if run_already
end

Public Instance Methods

new_position?() click to toggle source
#

new_position?

#
# File lib/chemistry_paradise/show_element.rb, line 96
def new_position?
  @relative_position
end
opnn() click to toggle source
#

opnn

#
Calls superclass method ChemistryParadise::Base#opnn
# File lib/chemistry_paradise/show_element.rb, line 133
def opnn
  super(NAMESPACE)
end
process_input() click to toggle source
#

process_input

#
# File lib/chemistry_paradise/show_element.rb, line 71
def process_input
  if    @input.include? '+'
    set_batch('+')
  elsif @input.include? '-'
    set_batch('-')
  else
    set_name_of_the_element(@input)
  end
end
report_result() click to toggle source
#

report_result

#
# File lib/chemistry_paradise/show_element.rb, line 103
def report_result
  current_position = PERIODIC_TABLE[@name_of_the_element]
  current_position = current_position + new_position?
  opnn; e PERIODIC_TABLE.invert[current_position]+' is at position '+
          sfancy(current_position.to_s)+'.'
end
reset() click to toggle source
#

reset

#
# File lib/chemistry_paradise/show_element.rb, line 45
def reset # (reset tag)
  set_name_of_the_element
  set_relative_position
end
run() click to toggle source
#

run

#
# File lib/chemistry_paradise/show_element.rb, line 126
def run # (run tag)
  report_result
end
set_batch(on_which_token = '+') click to toggle source
#

set_batch

This will set both (1) the name and (2) the relative position.

#
# File lib/chemistry_paradise/show_element.rb, line 86
def set_batch(on_which_token = '+')
  _ = @input.split(on_which_token)
  set_name_of_the_element _[0]
  _[1] = '-'+_[1].to_s if on_which_token == '-'
  set_relative_position   _[1]
end
set_input(i = '') click to toggle source
#

set_input

#
# File lib/chemistry_paradise/show_element.rb, line 53
def set_input(i = '')
  i = i.first if i.is_a? Array
  i = i.to_s.dup if i
  @input = i
  process_input
end
set_name_of_the_element(i = nil) click to toggle source
#

set_name_of_the_element

#
# File lib/chemistry_paradise/show_element.rb, line 113
def set_name_of_the_element(i = nil)
  # ======================================================================= #
  # if the input is a number, we invert the PERIODIC_TABLE.
  # ======================================================================= #
  if i =~ /^\d+$/
    i = PERIODIC_TABLE.invert[i.to_i]
  end
  @name_of_the_element = i
end
set_relative_position(i = 0) click to toggle source
#

set_relative_position

#
# File lib/chemistry_paradise/show_element.rb, line 63
def set_relative_position(i = 0)
  i = i.to_i if i
  @relative_position = i
end