Ruby "end" statement problems -
i stuck program won't run properly. here code:
puts '<input greeting below>' answer = gets.chomp if answer == 'hello' || 'hi' answer == true else answer == false puts 'hey, how you?' answer2 = gets.chomp if answer2 == 'i\'m good' || 'i\'m doing well' answer2 == true else answer2 == false puts 'that\'s good. know facts ruby programming?' answer3 = gets.chomp if answer3 == 'sure' answer3 == true else answer3 == false puts 'ok, did know {hello, world} first program ever made?' answer4 = gets.chomp if answer4 == 'yes' answer4 == true else answer4 == false puts 'wow, you\'re pretty good! know fact?' answer5 = gets.chomp if answer5 == 'sure' answer5 == true else answer5 == false puts 'alright, did know programming language "ruby" developed japanese techonolgist named "yukihiro matsumoto" because wasn\'t satisfied other programming languages?' end end end end end windows cmd says have problem on line 29 "end" part. can't figure out. can please help?
you've got number of problems here need fixed before can working program.
first up, if x == || b not mean think does. evaluates if x == (a || b) compare first string, not second. test against multiple possible matches best approach use case:
case (answer) when 'hello', 'hi' # matches! else # not matched end you can break out several different conditions adding additional when clauses, , can use regular expressions catch variations in case, , forth.
as added in comment, nested if pattern needs go away. need switch state system instead:
state = :greeting loop case (state) when :greeting puts "<input greeting below>" case (gets.chomp) when "hello", "hi" state = :how_are_you else break end when :how_are_you puts "hey, how you?" case (gets.chomp) when "i'm good", "i'm doing well" state = :thats_good else break end # ... additional `when` clauses. end end
Comments
Post a Comment