Forum: Ruby Newbie looking for help: Determining Gender

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Matthew B. (Guest)
on 2007-07-18 15:07
(Received via mailing list)
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
Stefan R. (Guest)
on 2007-07-18 15:22
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
John B. (Guest)
on 2007-07-18 15:39
(Received via mailing list)
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,
F. Senault (Guest)
on 2007-07-18 15:58
(Received via mailing list)
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
Robert D. (Guest)
on 2007-07-18 15:59
(Received via mailing list)
On 7/18/07, John B. <removed_email_address@domain.invalid> 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 ;)

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

just my micromoney
R.
James G. (Guest)
on 2007-07-18 17:23
(Received via mailing list)
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
Giles B. (Guest)
on 2007-07-19 01:20
(Received via mailing list)
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
Peña, Botp (Guest)
on 2007-07-19 06:31
(Received via mailing list)
From: Matthew B. [mailto:removed_email_address@domain.invalid]
# 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 :)

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
unknown (Guest)
on 2007-07-19 06:46
(Received via mailing list)
In message <removed_email_address@domain.invalid>,
=?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
Matthew B. (Guest)
on 2007-07-19 08:36
(Received via mailing list)
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." <removed_email_address@domain.invalid> wrote in message
news:removed_email_address@domain.invalid...
Chad P. (Guest)
on 2007-07-19 09:08
(Received via mailing list)
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*.
Gregory B. (Guest)
on 2007-07-19 09:14
(Received via mailing list)
On 7/19/07, Matthew B. <removed_email_address@domain.invalid> 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. :)
Chad P. (Guest)
on 2007-07-19 10:24
(Received via mailing list)
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).
Peña, Botp (Guest)
on 2007-07-19 12:17
(Received via mailing list)
From: Peter S. [mailto:removed_email_address@domain.invalid]
# >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 :)
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
This topic is locked and can not be replied to.