Skip to content

Instantly share code, notes, and snippets.

@sapereau83
Last active March 10, 2020 19:56
Show Gist options
  • Save sapereau83/94f483623d7458a53518dbe3c8242571 to your computer and use it in GitHub Desktop.
Save sapereau83/94f483623d7458a53518dbe3c8242571 to your computer and use it in GitHub Desktop.
class ChristmasTree
FIRST_LINE_LENGTH = 1
LEVEL_HEIGHT = 3
def initialize(levels)
@levels = levels.to_i < 1 ? LEVEL_HEIGHT : levels.to_i
@last_line_length = 5 + ((@levels - 1) * 2)
@current_line_length = FIRST_LINE_LENGTH
end
def draw_level
(1..LEVEL_HEIGHT).each do |line|
puts ("*" * @current_line_length).center(@last_line_length)
@current_line_length += 2 unless line == 3
end
@current_line_length = @current_line_length - 2
end
def draw_trunk
trunk_width = trunk_height = ((@last_line_length / 2) - 2).round
(1..trunk_height).each { puts ("|" * trunk_width).center(@last_line_length) }
end
def draw
(1..@levels).each { draw_level }
draw_trunk
end
end
ChristmasTree.new(ARGV.first).draw
@sapereau83
Copy link
Author

Feel free to visit my new project here

Merry Xmas!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment