Loops

Discussion in 'Ruby' started by Bruce Wayner, Aug 8, 2010.

  1. Bruce Wayner

    Bruce Wayner Guest

    Hi, I'm only a newbie on ruby could someone help me with my code:
    Here's: I'm writing a program which will ask the user to input a word or
    typed goodbye to quit the program. The first part is done the problems
    is that how can i find the longest word?
    example:
    Type a word (or goodbye to quit): Hello
    Type a word (or goodbye to quit): Hello Hello
    Type a word (or goodbye to quit): goodbye

    The longest word is “Hello Helloâ€

    can someone help me with my simple program?

    Code:
    loop do
    puts "Type a word ( or goodbye to quit):"
    $_ = gets.chomp

    break if $_ == "goodbye"

    end

    Attachments:
    http://www.ruby-forum.com/attachment/4923/test.rb

    --
    Posted via http://www.ruby-forum.com/.
     
    Bruce Wayner, Aug 8, 2010
    #1
    1. Advertising

  2. Bruce Wayner

    Damjan Rems Guest

    longest = ''
    loop do
    puts "Type a word ( or goodbye to quit):"
    $_ = gets.chomp
    break if $_ == "goodbye"
    longest = $_ if $_.size > longest.size
    end
    p "Longest is #(longest)"
    --
    Posted via http://www.ruby-forum.com/.
     
    Damjan Rems, Aug 8, 2010
    #2
    1. Advertising

  3. Bruce Wayner

    Bruce Wayner Guest

    Damjan Rems wrote:
    > longest = ''
    > loop do
    > puts "Type a word ( or goodbye to quit):"
    > $_ = gets.chomp
    > break if $_ == "goodbye"
    > longest = $_ if $_.size > longest.size
    > end
    > p "Longest is #(longest)"



    Thank U very much! for the fast response
    sorry i forgot to include if longest word has been select how can i get
    the length of that longest word example: Helo Helo (9 characters)

    Cheers
    --
    Posted via http://www.ruby-forum.com/.
     
    Bruce Wayner, Aug 8, 2010
    #3
  4. Hello,

    > p "Longest is #(longest)"


    You need accolades and not parenthesis here.
    Also, 'puts' might be better in this case ('p' uses 'inspect' and will
    display the string between "..")

    puts "Longest is #{longest}"

    Cheers,


    --=20
    JJ Fleck
    PCSI1 Lyc=E9e Kl=E9ber
     
    Jean-Julien Fleck, Aug 8, 2010
    #4
  5. Bruce Wayner

    Bruce Wayner Guest

    Bruce Wayner, Aug 8, 2010
    #5
  6. Bruce Wayner wrote:
    > Damjan Rems wrote:
    >> longest = ''
    >> loop do
    >> puts "Type a word ( or goodbye to quit):"
    >> $_ = gets.chomp
    >> break if $_ == "goodbye"
    >> longest = $_ if $_.size > longest.size
    >> end
    >> p "Longest is #(longest)"

    >
    >
    > Thank U very much! for the fast response
    > sorry i forgot to include if longest word has been select how can i get
    > the length of that longest word example: Helo Helo (9 characters)
    >
    > Cheers


    Change p "Longest is #(longest)" with
    puts "Longest is #(longest) of size #{longest.size}"

    HTH gfb
    --
    Posted via http://www.ruby-forum.com/.
     
    Gianfranco Bozzetti, Aug 8, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. eismaus4

    to many FOR loops?

    eismaus4, Apr 27, 2004, in forum: VHDL
    Replies:
    1
    Views:
    691
  2. Jeremy Morton

    Perl loops should use break, not last

    Jeremy Morton, Jan 29, 2005, in forum: Perl
    Replies:
    1
    Views:
    5,151
    Big and Blue
    Jan 30, 2005
  3. Multiple For Loops?

    , Feb 22, 2006, in forum: VHDL
    Replies:
    1
    Views:
    474
    john Doef
    Feb 22, 2006
  4. =?Utf-8?B?Um9iIE8nQ29w?=

    RedirectFromLoginPage and loops

    =?Utf-8?B?Um9iIE8nQ29w?=, Oct 25, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    412
    =?Utf-8?B?Um9iIE8nQ29w?=
    Oct 25, 2004
  5. Me
    Replies:
    2
    Views:
    249
Loading...

Share This Page