On 21 Aug 2001 07:19:47 +0900, Joshua Drake wrote:
> Hello,
> 
> I am the author of the Programming in ruby on IBM Developworks. I have been
> reviewing all the feedback I received on the first article. Most of the
> feedback was positive but I did receive some negative feedback about
> my use of Global variables, and the over simplified approach I used
> for some of the examples.


[CUT]

> == Begin Script
> #!/usr/bin/ruby
> begin
>  myfile = open('phonespec.txt', 'a')
>  $closing = "\n\nTo start the program again please type intro2.rb\n\tGood 
> Bye!\n\n"
> 
>     enterFirstName = ""    #First Name in the addressbook
>     enterLastName = ""    #Last Name in the Addressbook
>     enterPhoneNumber = "" #Phone Number in the addressbook
> 
>   print "Welcome to MyAddressbook. Please follow the prompts.\n"
>   print "If you wish to end data entry, you may do so at any time\n"
>   print "by type the word END into a prompt.\n\n"
>  while 1
>    print "\n\tFirst Name: "
>    enterFirstName = STDIN.gets
>    enterFirstName.chop!
>    if enterFirstName == "END"
>      print $closing
>      break
>    end
>    if enterFirstName != "END"
>      print "\n\tLast Name: "
>      enterLastName = STDIN.gets
>      enterLastName.chop!
>   end
>   if enterLastName == "END"
>     print $closing
>     break
>   end
>   if enterLastName != "END"
>     print "\n\tPhone Number: "
>     enterPhoneNumber = STDIN.gets
>     enterPhoneNumber.chop!
>   end
>   if enterPhoneNumber == "END"
>     print $closing
>     break
>   end
>   myfile.write(enterFirstName+"\t"+enterLastName+"\t"+enterPhoneNumber+"\n")
>  end
>    ensure
>      myfile.close
> end

Typing:
if a == "END"
   ...
end
if a != "END"
   ...
end

looks BAD.

This is a little better
if a == "END
   ....
else
   ....
end


Assuming you don't want to use to much ruby-special-stuff you can do
this in the loop....

print \n\tFirst Name: "
firstName = STDIN.gets.chop
break if firstName == "END"
print "\n\tLast Name:"
lastName = STDIN.get.chop
break if lastName == "END"
print "\n\tPhone Number: "
phoneNumber = STDIN.get.chop
break if phoneNumber == "END"

looks alot better, is easier to read and understand.

Ofcourse.. if you want to exit the application you should create a
method like this

def get_input(string)
   print "\n\t#{string}: "
   tmp = STDIN.gets.chop
   exit 0 if tmp == "END"
   tmp
end

now you can do

firstName = get_input("First Name")
lastName = get_input("Last Name")
phoneNumber = get_input("Phone Number")

The usual disclaimer for untested code applies.

I think you examples only shows ruby in a bad way.  Experienced
programmers that looks at ruby for the first time will not look into it
deeper... sorry...



/Erik

-- 
Erik BéČfors               | erik / bagfors.nu
Supporter of free software | GSM +46 733 279 273
fingerprint: 6666 A85B 95D3 D26B 296B 6C60 4F32 2C0B 693D 6E32