|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
|
|
#1
|
|||
|
|||
|
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
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Ruby Programming > Wierd problem with if statements |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|