6 Replies - 9341 Views - Last Post: 25 January 2017 - 01:03 PM Rate Topic: -----

#1 rogersj7  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 24-October 08

Making shapes with ruby.

Post icon  Posted 25 October 2008 - 05:43 PM

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.
Is This A Good Question/Topic? 0
  • +

Replies To: Making shapes with ruby.

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5072
  • View blog
  • Posts: 13,694
  • Joined: 18-April 07

Re: Making shapes with ruby.

Posted 25 October 2008 - 08:05 PM

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



Essentially we ask for a number, loop from 1 to that number and construct the proper number of spaces and asterisks for each level until we get to the middle. Then we do the reverse to form the bottom half of the diamond.

As with all ruby this can be crunched down even further, but I wanted to keep this open enough so it wasn't too cryptic.

Hope it helps!

"At DIC we be diamond building with ruby code ninjas... we are the jewelers of the coding world." :snap:
Was This Post Helpful? 0
  • +
  • -

#3 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Making shapes with ruby.

Posted 25 January 2017 - 12:39 PM

what does the i.upto do in that coding? and what if i wanted to only make the diamond shape have asteriks on the outside of the shape and not filled in like that coding there? How would I work with that?
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13482
  • View blog
  • Posts: 53,820
  • Joined: 12-June 08

Re: Making shapes with ruby.

Posted 25 January 2017 - 12:48 PM

I believe you already have a more current thread open on that topic. No need to dredge up 2008.
Was This Post Helpful? 0
  • +
  • -

#5 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Making shapes with ruby.

Posted 25 January 2017 - 12:55 PM

not on making diamonds, that one is for a reverse triangle which i still don't get, however this diamond uses an i.upto code and I was curious on that coding and i didn't need the i.upto for my code, maybe that would be a better and neater way to do that code i needed help with. So i asked, didn't know i couldn't bring up old forums to try and learn something..
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13482
  • View blog
  • Posts: 53,820
  • Joined: 12-June 08

Re: Making shapes with ruby.

Posted 25 January 2017 - 01:01 PM

:rolleyes2: Ooookay... it read like an extension of your existing thread.
Was This Post Helpful? 0
  • +
  • -

#7 jstanley6  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 39
  • Joined: 22-January 17

Re: Making shapes with ruby.

Posted 25 January 2017 - 01:03 PM

ohh sorry, I'm bad at doing threads in here. Not even sure how to edit threads so i just reply to my threads already posted with an updated version of what I was trying to say
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1