Newbie looking for help: Determining Gender

Hey All-

I just started attempting to program in Ruby a month ago, but have
reached
an impasse.

I am writing a dosing program and am trying to do something very simple:
Have a user enter either m for a male or f for a female so that info can
be
taken to the next equation to be used for something else.

I originally wrote it as follows:

#Introduction

puts ‘Hello. Welcome to the Aminoglycoside Dosing Calculator.’

Determines the patient dosing gender

puts ‘What gender is your patient? Please enter m or f’
ptSexS = gets.chomp.downcase
if ptSexS == ‘f’
doseSexS = ‘female’
elsif ptSexS == ‘m’
doseSexS = ‘male’
end
puts 'Your patient is a ’ + doseSexS

The problem here is I have written all of the mathematical operations I
need
in blocks like this, but I dont know how to make them accomodate for
invalid
answers.
For instance, if the user enters an m or an f above, I have the doseSexS
determined for the next part of the program. If the user enters
something
else, I want it to ay "please try again’ and start over…

I have tried while loops but cant get it to work as well as making it
into a
method. Is there a different way to do this that my lack of experience
is
preventingme from seeing/ Is iteration over a an array something worth
following? I apologize for such a basic question, but the PickAxe,the
book
by Chris P., and multiple web sites are failing me and I am turnig
here as
a last resort…

Thanks in advance for any help

Matthew F Borgeson

On 18 Jul 2007, at 12:05, Matthew B. wrote:

taken to the next equation to be used for something else.
if ptSexS == ‘f’
for invalid
experience is

You might try looking at using either raise … rescue or throw …
catch to deal with errors. Discussion on p 360 of pickaxe and elsewhere.

in your code something like

if f
elsif m
else
raise
end

with rescue elsewhere to do what you want with the error.

Hope this helps.

allbests,

Matthew B. wrote:

Hey All-

puts ‘What gender is your patient? Please enter m or f’
ptSexS = gets.chomp.downcase
if ptSexS == ‘f’
doseSexS = ‘female’
elsif ptSexS == ‘m’
doseSexS = ‘male’
end
puts 'Your patient is a ’ + doseSexS

dose_sex = nil
until dose_sex
print 'What gender is your patient? Please enter m or f: ’
answer = gets.chomp.downcase
if answer == ‘f’
dose_sex = ‘female’
elsif answer == ‘m’
dose_sex = ‘male’
end
puts “Invalid answer, please try again.” unless dose_sex
end
puts “Your patient is a #{dose_sex}.”

You also may want to take a look at Highline or similar libraries which
help making TUIs.

Regards
Stefan

Le 18 juillet à 03:57, Matthew B. a écrit :

BTW, to go further into rubyisms, I believe this is the typical
situation where symbols should be used (eg. dose_sex = :female).

My way :

dose_sex = nil
until dose_sex
print 'What gender is your patient (please enter m or f) : ’
dose_sex = case gets.chomp.downcase
when ‘f’ then :female
when ‘m’ then :male
else puts “Please only enter m or f.”
end
end

puts “You chose #{dose_sex.to_s}.”

HTH !

Fred

On Jul 18, 2007, at 6:05 AM, Matthew B. wrote:

The problem here is I have written all of the mathematical
operations I need
in blocks like this, but I dont know how to make them accomodate
for invalid
answers.

I recommend using a library to make sure you only get valid answers:

Firefly:~/Desktop$ ruby gender.rb
What gender is your patient? dog
You must choose one of [:male, :female].
? male
:male
Firefly:~/Desktop$ ruby gender.rb
What gender is your patient? f
:female
Firefly:~/Desktop$ cat gender.rb
#!/usr/bin/env ruby -wKU

require “rubygems”
require “highline/import”

gender = ask("What gender is your patient? ", [:male, :female])
p gender

END

James Edward G. II

The subject of this thread is a great subject. It’s like the ultimate
newbie question.


Giles B.

Blog: http://gilesbowkett.blogspot.com
Portfolio: http://www.gilesgoatboy.org

On 7/18/07, John B. [email protected] wrote:

simple:

The problem here is I have written all of the mathematical
I have tried while loops but cant get it to work as well as making
Thanks in advance for any help
in your code something like

allbests,

personally I think this is a case 4 case :wink:

case sex
when /f(?:emale)?/i
s = ‘female’
when /m(?:ale)?/i
s=“male”
else
errorhandling

just my micromoney
R.

In message [email protected],
=?iso-8859-1?Q?Pe=F1a=2C_Botp?=
writes:

sexCheckH = Hash.new
sexCheckH = { “m” => “male”,
“f” => “female”
}

It seems to me that you have just replaced your empty Hash.new object
with a
brand new object.

doseSexS = nil
until doseSexS do
puts ‘What gender is your patient? Please enter m or f’
doseSexS = sexCheckH[ptSexS = gets.chomp.downcase]
puts “Invalid entry [#{ptSexS}]. Pls retry” unless doseSexS
end
puts “Your patient is a #{doseSexS}.”

This looks like it should work, though.

-s

Hey Giles-

I hope this doesn’t mean that I didnt waste everyone’s time here.

I learn better from examples, and there werent really any explicit
examples
in either reference, but implicit ones I had to extract and play around
with
on my own.

That being said, after trying Python and C++ by myself and no classes
nor
budget for M$ development tools, Ruby seemsjust like the tool I need to
learn to program.

“Giles B.” [email protected] wrote in message
news:[email protected]

On Thu, Jul 19, 2007 at 01:35:01PM +0900, Matthew B. wrote:

learn to program.
For my money, the best languages for learning programming on your own
are
(depending on what type of programming you want to learn first), in no
particular order:

Logo
Perl
Ruby

Assembly language might also be a good choice, but it takes a pretty
special student to do that first.

On 7/19/07, Matthew B. [email protected] wrote:

Hey Giles-

I hope this doesn’t mean that I didnt waste everyone’s time here.

Not at all, you could have just picked a better subject. I personally
think that putting the word newbie in your post is not necessary, so
that leaves you with “Determining Gender”, which of course, is
meaningless.

Your question really was

“How do I validate command line input?”

Of course, being new to Ruby, or to programming, might have made it
tough for you to think of a good subject line. Not a big deal, just
worth keeping in mind for next time.

The subject you select for your posts is very important, most people
select what they’d like to read almost entirely on subject alone.

The question itself, was just fine, and the others advice is all good.
Being a maintainer on HighLine, I’ll second the notion that it’s the
right tool for the job. :slight_smile:

On Thu, Jul 19, 2007 at 02:12:54PM +0900, Gregory B. wrote:

The subject you select for your posts is very important, most people
select what they’d like to read almost entirely on subject alone.

On the plus side, at least in this case the error seems to be on the
side
of getting people to read it (out of curiosity, if nothing else).

From: Matthew B. [mailto:[email protected]]

I have tried while loops but cant get it to work as well as

making it into a method. Is there a different way to do this

that my lack of experience is preventingme from seeing/ Is

iteration over a an array something worth following? I

apologize for such a basic question, but the PickAxe,the book

by Chris P., and multiple web sites are failing me and I am

turnig here as a last resort…

the pickaxe and pine’s book are good enough. one only needs practice,
think like exercise or kata :slight_smile:

if a lot if/else boggles your mind (maybe because of deciding when to
loop back and what to print), you might want to delegate the sex choice
to a list checker, say like “check entry from a list. do foo if not
found otherwise do blah…”; thus letting ruby do the search logic for
us and we only take care of the loop problem …

eg,

botp@pc4all:~$ cat test.rb
#------------------------------------
sexCheckH = Hash.new
sexCheckH = { “m” => “male”,
“f” => “female”
}

doseSexS = nil
until doseSexS do
puts ‘What gender is your patient? Please enter m or f’
doseSexS = sexCheckH[ptSexS = gets.chomp.downcase]
puts “Invalid entry [#{ptSexS}]. Pls retry” unless doseSexS
end
puts “Your patient is a #{doseSexS}.”
#------------------------------------

botp@pc4all:~$ ruby test.rb
What gender is your patient? Please enter m or f
x
Invalid entry [x]. Pls retry
What gender is your patient? Please enter m or f
m
Your patient is a male.
botp@pc4all:~$ ruby test.rb
What gender is your patient? Please enter m or f
y
Invalid entry [y]. Pls retry
What gender is your patient? Please enter m or f
f
Your patient is a female.
botp@pc4all:~$

kind regards -botp

From: Peter S. [mailto:[email protected]]

>sexCheckH = Hash.new

>sexCheckH = { “m” => “male”,

> “f” => “female”

> }

It seems to me that you have just replaced your empty

Hash.new object with a

brand new object.

argggh, i was hoping that the op would catch that first :slight_smile:
while typing code, i was thinking whether showing a hash will complicate
the strategy to his looping problem … so i figure how to type the hash
so it looks simple, and it went like…

x=Hash.new … hmmm, no
x={}
x[‘m’] = ‘male’ … hmmm, no, change mind again…
x = { ‘m’ => ‘male’, … } yes

next time, i’ll just type directly without thinking :))

thanks for the heads up and kind regards -botp