#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    1
    Rep Power
    0

    Wierd problem with if statements


    Hello, first post here...

    Well I've just(today) started using Ruby, but it's not my first programming language...

    When just playing a bit with the basics, and got a really weird problem, I can't get the if statements to work...

    I have this code.

    Code:
    name = gets()
    puts '#{name}'
    
    if name == 'Rune' then
         puts 'hello'
    elsif name == 'Casper' then
         puts 'fork'
    else
         puts 'it didn't work'
    end
    I run it and type in Rune, and i get this output:
    Code:
    Rune
    it didn't work
    The variable does contain the right string, but it skips to the else code-block... I just can't seem to figure out what's wrong!
    I've tried using the case statement, same thing happened, just skips to the else statement... (I've also tried the different syntaxes of the statements)

    Please, can someone tell my what's wrong..?

    Best regards Rune B
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2007
    Location
    Melbourne, Australia.
    Posts
    9
    Rep Power
    0
    Try this:
    irb(main):002:0> name = gets
    steve
    => "steve\n"
    irb(main):003:0> name = gets.chomp
    steve
    => "steve"

    Try using 'gets.chomp' rather than 'gets'. As you see from the above, if only using 'gets' the return value includes the next line character.

IMN logo majestic logo threadwatch logo seochat tools logo