: 
http://rubylearning.com/
http://rubylearning.com/satishtalim/tutorial.html
http://rubylearning.com/download/downloads.html
http://rubylearning.com/other/certification.html
http://rubylearning.com/blog/
http://rubylearning.com/other/ruby_news.html
http://rubylearning.com/other/testimonials.html
http://rubylearning.com/jobs/ruby_jobs.html
http://rubylearning.com/other/ruby_gurus.html
http://rubylearning.com/satishtalim/services.html
http://rubylearning.com/contact/contact.html
http://rubylearning.com/satishtalim/about.html


Random Numbers

Ruby comes with a random number generator. The method to get a randomly chosen number is rand. If you call rand, you'll get a float greater than or equal to 0.0 and less than 1.0. If you give it an integer parameter (by calling rand(5) ), you will get an integer value greater than or equal to 0 and less than 5.

Here's an example: p026phrase.rb

word_list_one = ['24/7', 'multi-Tier', '30,000 foot', 'B-to-B', 'win-win', 'front-end', 'web-based', 'pervasive', 'smart', 'six-sigma', 'critical-path', 'dynamic']

word_list_two = ['empowered', 'sticky', 'value-added', 'oriented', 'centric', 'distributed', 'clustered', 'branded', 'outside-the-box', 'positioned', 'networked', 'focused', 'leveraged', 'aligned', 'targeted', 'shared', 'cooperative', 'accelerated']

word_list_three = ['process', 'tipping-point', 'solution', 'architecture', 'core competency', 'strategy', 'mindshare', 'portal', 'space', 'vision', 'paradigm', 'mission']

one_len = word_list_one.length

two_len = word_list_two.length

three_len = word_list_three.length

rand1 = rand(one_len)

rand2 = rand(two_len)

rand3 = rand(three_len)

phrase = word_list_one[rand1] + " " + word_list_two[rand2] + " " + word_list_three[rand3]

puts phrase

The above program makes three lists of words, and then randomly picks one word from each of the three lists and prints out the result.


Assignment:

This assignment is from Chris Pine's Book.

  1. Write a Deaf Grandma program (p026zdeafgm1.rb). Whatever you say to grandma (whatever you type in), she should respond with HUH?!  SPEAK UP, SONNY!, unless you shout it (type in all capitals). If you shout, she can hear you (or at least she thinks so) and yells back, NO, NOT SINCE 1938! To make your program really believable, have grandma shout a different year each time; maybe any year at random between 1930 and 1950. You can't stop talking to grandma until you shout BYE.
  2. Program (p026zdeafgm2.rb) - Extend your Deaf Grandma program: What if grandma doesn't want you to leave? When you shout BYE, she could pretend not to hear you. Change your previous program so that you have to shout BYE three times in a row. Make sure to test your program: if you shout BYE three times, but not in a row, you should still be talking to grandma.




Learning Ruby Navigation                                                                    <Blocks and Procs  | TOC  |  Read/Write files>