Skip to content

Instantly share code, notes, and snippets.

@huezoaa
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huezoaa/42a306576edbad210bee to your computer and use it in GitHub Desktop.
Save huezoaa/42a306576edbad210bee to your computer and use it in GitHub Desktop.
Christmas Tree w/ Class
# Dislays the specified character in the shape of a
# Christmas tree.
# Wyncode bootcamp. Ruby Classes and OOP
# Arguments for Christmas tree:
# size = number of lines (Fixnum)
# character = string character to use (String)
class ChristmasTree
def initialize(size=3, character="X")
@size = size
@character = character
@odd_num=0
@branch_array=[]
end
# Array with the odd numbers which represent how many
# characters to print on each branch of the Christmas tree
# Odd numbers work best for the Christmas tree shape!
def create_array
until @branch_array.length >= @size do
if @odd_num.odd?
@branch_array << @odd_num
@odd_num += 1
else
@odd_num += 1
end
end
return @branch_array
end
# Method to display the Christmas tree shape. It takes the
# @branch_array array and prints blank spaces before the
# odd number of characters.
def display
@branch_array = create_array
for n in @branch_array
((@odd_num-1-n)/2).times{print " "}
n.times{print @character}
print "\n"
end
end
end
# Tests of object creation and methods.
t1 = ChristmasTree.new
t1.create_array
p t1
t2 = ChristmasTree.new(20,"R")
t2.display
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment