forked from Ada-C11/Solar-System
-
Notifications
You must be signed in to change notification settings - Fork 0
/
planet.rb
41 lines (34 loc) · 996 Bytes
/
planet.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# planet.rb
require "terminal-table"
class Planet
attr_reader :name, :color, :mass_kg, :distance_from_sun_km, :fun_fact
def initialize(name, color, mass_kg, distance_from_sun_km, fun_fact)
@name = name
@color = color
@mass_kg = mass_kg
@distance_from_sun_km = distance_from_sun_km
@fun_fact = fun_fact
end
def mass_kg
pretty_mass = @mass_kg.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
return pretty_mass
end
def distance_from_sun
pretty_distance = @distance_from_sun_km.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
return pretty_distance
end
def summary
table = Terminal::Table.new do |t|
t << ["Name:", @name]
t << :separator
t << ["Color:", @color]
t << :separator
t << ["Mass:\n(kilograms)", mass_kg]
t << :separator
t << ["Distance from the sun:\n(kilometers)", distance_from_sun]
t << :separator
t << ["Fun Fact:", @fun_fact]
end
return table
end
end