How do you modify excessive user input in Ruby? -
i know seems quite personal, have question modifying user input.
so want create "code-language" decoder, parts of correction isn't corrected correctly. think might because there many gsubs.
an example of have:
puts "hello world:" user_input = gets.chomp user_input.downcase! if user_input.include? "a" user_input.gsub!(/a/, "b") user_input.include? "b" user_input.gsub!(/b/, "c") user_input.include? "c" user_input.gsub!(/c/, "d") user_input.include? "d" user_input.gsub!(/d/, "e") user_input.include? "e" user_input.gsub!(/e/, "f") user_input.include? "f" user_input.gsub!(/f/, "g") user_input.include? "g" user_input.gsub!(/g/, "h") user_input.include? "h" user_input.gsub!(/h/, "i") user_input.include? "i" user_input.gsub!(/i/, "j") user_input.include? "j" user_input.gsub!(/j/, "k") user_input.include? "k" user_input.gsub!(/k/, "l") user_input.include? "l" user_input.gsub!(/l/, "m") user_input.include? "m" user_input.gsub!(/m/, "n") user_input.include? "n" user_input.gsub!(/n/, "o") user_input.include? "o" user_input.gsub!(/o/, "p") user_input.include? "p" user_input.gsub!(/p/, "q") user_input.include? "q" user_input.gsub!(/q/, "r") user_input.include? "r" user_input.gsub!(/r/, "s") user_input.include? "s" user_input.gsub!(/s/, "t") user_input.include? "t" user_input.gsub!(/t/, "u") user_input.include? "u" user_input.gsub!(/u/, "v") user_input.include? "v" user_input.gsub!(/v/, "w") user_input.include? "w" user_input.gsub!(/w/, "x") user_input.include? "x" user_input.gsub!(/x/, "y") user_input.include? "y" user_input.gsub!(/y/, "z") user_input.include? "z" user_input.gsub!(/z/, "a") end puts "#{user_input}"
thanks helping!
several problems here.
first of all, if
statements in ruby don't work way appear think do. can put condition on line after if
keyword (though it's not recommended), after 'body' of statement (the part gets executed if condition true) continues until reach end
keyword. indentation means nothing. long input contains a
, of gsub!
lines run. if doesn't contain a
, nothing afterwards except puts
run.
second, gsub!
modifies receiver. user's input abcabc
. first gsub!
call replaces a
s b
s, afterwards user_input
bbcbbc
. second gsub!
call replaces b
s c
s, including ones used a
s, afterwards user_input
cccccc
. end of program, every letter in string has been changed z
, , of them changed a
. things remain non-letter characters such spaces, punctuation, numbers, etc.
(third, don't need check see if string includes particular letter before running gsub!
. if user_input
doesn't contain b
, say, user_input.gsub!(/b/,'c')
won't anything.)
Comments
Post a Comment