Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Tutorial: Decisions in Ruby


  • Please log in to reply
2 replies to this topic

#1 mr mike

mr mike

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 89 posts

Posted 22 December 2010 - 01:42 PM

This tutorial will show how ruby makes decisions using while and until through a snippet of code.

#output will display nil
x = x + 1 while x < 10

#output will display 10
puts x
 
#output nil
x = x - 1 until x == 1

#output 1
puts x

#output will print  2 - 10
while x < 10
     x = x + 1
     puts x
end

Looking at the above code we can notice some aspects of ruby. The first block iterated through while x was less than 10 yet displayed 10. The second block of code subtracted 1 until x equaled 1. Finally the while loop displayed the numbers:
2
3
4
5
6
7
8
9
10

Though this is not a traditional tutorial using detailed explanation, the above snippets show the basics of making decisions that are straightforward. I will be posting more examples in the future.

Edited by Roger, 06 January 2011 - 12:00 PM.

  • 0

#2 mrlemke

mrlemke

    CC Resident

  • Advanced Member
  • PipPipPipPip
  • 68 posts

Posted 25 December 2010 - 08:31 AM

So, you showed me some loops. What's the decision here?

Edited by daniel-lemke, 31 December 2010 - 11:19 AM.

  • 0

#3 mr mike

mr mike

    CC Resident

  • Just Joined
  • PipPipPipPip
  • 89 posts

Posted 30 December 2010 - 01:36 PM

Sorry about the misunderstanding. I was trying to show the simplicity of the syntax in ruby by using loops, which are decisions, the program decides when to stop running a block of code.
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download