[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: A very early Ruby problem..

Jonndailey

5/23/2007 4:53:00 PM

Thanks a lot. I was going about it the wrong way. It looks so simple
now. Thank you!

On May 23, 4:15 am, anansi <kaz...@oleco.net> wrote:
> Well this is rather simple. You know how to read from STDIN (= console
> input)? There are several ways like gets or readline. So
>
> surname = readline or surename = gets reads from the console and saves
> the string in the variable surname. You can get the length of a string
> with the length method. So surename.length gives the length of surename
> but realize that gets and readline also are saving the newline feed in
> the variable you have to cut off with chomp or length will always be 1
> to high.
>
> A simple implementation looks like this:
>
> #!/usr/bin/env ruby
>
> $VERBOSE = true
>
> # outputs surname on the screen
> puts "surname:"
> # reads console input to surname
> surname = gets
>
> puts "middlename:"
> middlename = gets
>
> puts "lastname:"
> lastname = gets
>
> puts "total length:"
> # adds the lengths of the three strings after they have beeen chomped
> p surname.chomp.length+middlename.chomp.length+lastname.chomp.length
>
>
>
> Jonndailey wrote:
> > In Chris Pine's book, "Learn to Program", he asks you to write a small
> > app. I'm having trouble writing this app and I was wondering if anyone
> > could help me out.
>
> > Here's the excerpt:
>
> > "I guess we could write a program which asks for your first, middle,
> > and last names individually, and then adds those lengths together...
> > hey, why don't you do that! Go ahead, I'll wait."
> >http://pine.fm/LearnToProgram/?...
>
> > I'm having a problem writing this code. I know a lot of you will look
> > at this and think this is the easiest thing ever. I hope I catch
> > someone like that to help me out.
>
> > Any ideas?
>
> > thanks a lot.
>
> --
> greets
>
> one must still have chaos in oneself to be able to
> give birth to a dancing star