Newbie looking for help: Determining Gender

M

Matthew Borgeson

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 Pine, 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
 
S

Stefan Rusterholz

Matthew said:
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
 
J

John Browning

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 Pine, 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

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,
 
F

F. Senault

Le 18 juillet à 03:57, Matthew Borgeson 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
 
R

Robert Dober

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,

personally I think this is a case 4 case ;)

case sex
when /f(?:emale)?/i
s = 'female'
when /m(?:ale)?/i
s="male"
else
errorhandling

just my micromoney
R.
 
J

James Edward Gray II

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 Gray II
 
G

Giles Bowkett

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

Peña, Botp

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

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

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 =3D Hash.new
sexCheckH =3D { "m" =3D> "male",
"f" =3D> "female"
}

doseSexS =3D nil
until doseSexS do
puts 'What gender is your patient? Please enter m or f'
doseSexS =3D sexCheckH[ptSexS =3D 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
 
M

Matthew Borgeson

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.
 
C

Chad Perrin

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.

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*.
 
G

Gregory Brown

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. :)
 
C

Chad Perrin

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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top