Class: LittleElementor::Element

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/little_elementor.rb

Constant Summary

Constants included from Helpers

Helpers::ELEMENTS, Helpers::L

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#get_e_c, #get_group

Constructor Details

#initialize(opts) ⇒ Element

Returns a new instance of Element.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/little_elementor.rb', line 9

def initialize(opts)
  opts = opts.has_key?(:charge) ? opts : opts.merge({:charge => 0})
  @charge = opts[:charge]
  if opts.has_key?(:atomic_number)
    @fake_atomic_number = opts[:atomic_number] 
    @atomic_number = @fake_atomic_number + @charge
    @symbol = LittleElementor::Helpers::ELEMENTS[@atomic_number-1][:sym]
  elsif opts.has_key?(:symbol)
    @atomic_number = LittleElementor::Helpers::ELEMENTS.find_index{|i| i[:sym] == opts[:symbol]} + 1
    @fake_atomic_number = @atomic_number + @charge
    @symbol = opts[:symbol]
  end
  @group = @atomic_number == 2 ? 18 : get_group()
  @is_nobel_gas = @group == 18
  @name = LittleElementor::Helpers::ELEMENTS[@atomic_number-1][:name]
  @period = electron_configuration.map(&:n).max
end

Instance Attribute Details

#atomic_numberObject (readonly)

Returns the value of attribute atomic_number.



8
9
10
# File 'lib/little_elementor.rb', line 8

def atomic_number
  @atomic_number
end

#chargeObject (readonly)

Returns the value of attribute charge.



8
9
10
# File 'lib/little_elementor.rb', line 8

def charge
  @charge
end

#fake_atomic_numberObject (readonly)

Returns the value of attribute fake_atomic_number.



8
9
10
# File 'lib/little_elementor.rb', line 8

def fake_atomic_number
  @fake_atomic_number
end

#groupObject (readonly)

Returns the value of attribute group.



8
9
10
# File 'lib/little_elementor.rb', line 8

def group
  @group
end

#is_nobel_gasObject (readonly)

Returns the value of attribute is_nobel_gas.



8
9
10
# File 'lib/little_elementor.rb', line 8

def is_nobel_gas
  @is_nobel_gas
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/little_elementor.rb', line 8

def name
  @name
end

#periodObject (readonly)

Returns the value of attribute period.



8
9
10
# File 'lib/little_elementor.rb', line 8

def period
  @period
end

#symbolObject (readonly)

Returns the value of attribute symbol.



8
9
10
# File 'lib/little_elementor.rb', line 8

def symbol
  @symbol
end

Instance Method Details

#electron_configuration(sorted: true) ⇒ Object



27
28
29
# File 'lib/little_elementor.rb', line 27

def electron_configuration(sorted: true)
  get_e_c(sorted)
end

#to_sObject



31
32
33
# File 'lib/little_elementor.rb', line 31

def to_s
  "Symbol: #{@symbol}\nis a noble gas?: #{@is_nobel_gas}\ncharge: #{@charge}\nname: #{@name}\natomic_number #{@atomic_number}\nsorted electron configuration: #{electron_configuration.map(&:to_s)}\nelectron configuration: #{electron_configuration(sorted: false).map(&:to_s)}\nperiod: #{@period}\ngroup: #{@group}\nfake_atomic_number: #{@fake_atomic_number}"
end