hi,
I'm new to the ruby universe. I like figuring out problems with ruby to get used to it but I'm having a problem finding code to make shapes such as diamonds, circles and such.
Any idea where I can find them? I know I will use loops (def and such) but any help is welcome.
J.
Well in ruby shape making is pretty easy compared to some of the other languages that require a lot more syntax. Here is a rough example of building an X width diamond...
# Prompt for the number of columns in the middle of a diamond
puts "Enter the number of columns for the diamond: "
count = gets.chomp.to_i
# Loop from 1 up to the count they entered
# In each loop we loop from that many spaces for the row, then that many asterisks
# End with a new line. This forms the top of the diamond.
1.upto(count) do |i|
i.upto(count - 1) { print " " }
i.times { print " *" }
print "\n"
end
# Subtract 1 so we don't double up the middle row
count = count - 1
# Now go from count down to 1
count.downto(1) do |i|
i.upto(count) { print " " }
i.times { print " *" }
print "\n"
end
Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)