[CoLoCo] [clue-talk] Re: Random Number Generators

Kevin Fries kfries at cctus.com
Fri Feb 22 16:31:33 GMT 2008


On Thu, 2008-02-21 at 22:26 -0700, Jim Hutchinson wrote:
> Kevin,
> 
> This is very cool. Thanks for doing it. I'll have Leina play with it
> some over the weekend (she's been kind of busy this week). One
> question though. I was sort of under the impression that the script
> would ask for user input regarding the number of rolls or the size of
> the dice, etc. I see now that the idea is to just edit the code which
> doesn't seem that hard. However, never having even tried to use ruby,
> what is involved in having user input for these variables? I don't
> want you to just add that, I'd rather play around with it myself, but
> I don't have clue how ruby works. I'm no python expert either but I
> think it would look something like this

Input in Ruby is child's play (variable = gets).  Even more than Python,
even more than Java, Ruby is OO.  If there is no object, its not in
Ruby, they take OO to insane proportions.  Also, all variables are
typeless.  So, lets say you want to ask for the number of sides on a
die:

sides4die = gets

easy enough.  But you need to insure that that value is a number, and
gets returns the enter key.  The chomp method takes care of the cr/lf
and the to_i insures whatever was types in converted into an integer.
Then you will want to insure the die has at least 4 sides, and if so,
set the property on the die.  I also want to allow an enter to just
accept the standard die.  So following my second example I may do
something like this:

sides4die = 0
until sides4die > 4
  puts "How many sides are on each die? [6]: "
  sides4die = gets.chomp.to_i
  sides4die = 6 if sides4die == 0
end

d1.sides = sides4die

There is allot going on there in 7 lines of code.

Let me point you to a simple webpage that will have you firing a UI in 2
minutes:
http://www.math.umd.edu/~dcarrera/ruby/0.3/chp_02/user_input.html

You can also always go to the rubyforge.org website for all kinds of
documentation.

Ruby has another tool which has no equivalent in either Python or Java
called IRB.  If you type it at the command line, you can enter my 3
classes in, then play to your hearts content.  Think of the old MS
Basic, where you could type in commands, and it would immediately
respond.  IRB is like that for Ruby.

-- 
Kevin Fries
Senior Linux Engineer
Computer and Communications Technology, Inc
A Division of Japan Communications Inc.



More information about the Ubuntu-us-co mailing list