I have the following code which works fine
if user_input.include? "s"
user_input.gsub!(/s/ "th")
else
print "Nothing to change"
end
But when I want to add another include like so it does not recognize the elsif How do I add these includes together?
if user_input.include? "s"
user_input.gsub!(/s/ "th")
elsif user_input.include? "cee"
user_input.gsub!(/cee/ "th")
else
print "Nothing to change"
end
Since gsub! returns nil if nothing changed, your can write your example just like this:
unless user_input.gsub!(/s|cee/ "th")
print "Nothing to change"
end
This is because of flow of execution of if else statement.
If condition in 'if' matches it will not execute 'elseif' block..
if user_input.include?('s') or user_input.include?('cee')
user_input.gsub!(/s/,"th").gsub!(/cee/,"th")
else
print "Nothing to change"
end
Your code show the error :
SyntaxError: unexpected ')', expecting keyword_end
You forget the commas in gsub
if user_input.include? "s"
user_input.gsub!(/s/, "th")
elsif user_input.include? "cee"
user_input.gsub!(/cee/, "th")
else
print "Nothing to change"
end
Edit :
If you want to make both replacement, you need to change to :
old_value = user_input
if user_input.include? "s"
user_input.gsub!(/s/, "th")
end
if user_input.include? "cee"
user_input.gsub!(/cee/, "th")
end
if user_input == old8value
print "Nothing to change"
end
Once the first if is matched, the rest are skipped.
For your particular use case, I would suggest that you use a single gsub like so:
regexp = /s|cee/
if string.match(regexp)
string.gsub!(regexp, "th")
else
"Nothing to gsub!"
end
Related
I want in brief to run a program to check if the user input is empty to let him reinsert the needed data and in case there is "s" in the string to be substituted with another letter
print "Please enter a string: "
user_input = gets.chomp.downcase!
if user_input.empty?
print "Please enter a vaild string... "
user_input = gets.chomp.downcase!
elsif
user_input.include? "s"
user_input.gsub!(/s/, "th")
else
puts "There are no 's's in your string. #{user_input}"
end
puts "Your new thtring is #{user_input}."
The problem is with this line
user_input = gets.chomp.downcase!
according to the docs
Downcases the contents of str, returning nil if no changes were made.
So if the user inputs a string with only lowercase letters, nil is returned.
Your function works if a user enters FOO, then it works fine.
You're better off using downcase instead of downcase!. downcase always return the string itself.
As I understand you need get valid user input (with s)
Now you are only using if and this does not guarantee that user input will be valid
You can refactor to something like this
puts "Please enter a string with s:"
thtring = ""
loop do
user_input = gets.chomp
next puts "Please enter some string..." if user_input.empty?
thtring = user_input.downcase
next puts "There are no 's's in your string" unless thtring.include?("s")
break thtring.gsub!(/s/, "th")
end
puts "Your new thtring is #{thtring}."
I am new to Ruby. My past is in Java. I am trying to use a switch case, apparently known as a case expression in Ruby. I want to accept user input, check that input to see if it includes certain characters, and then substitute those characters with other characters. When I try to run this simple program I get many syntax errors but I am not sure why. Could someone please explain to me if I am using this statement wrong and if I can even use case expression in this situation? Thank you.
empty_string = true
while empty_string do
print "Pleathe enter a thtring: "
user_input = gets.chomp
user_input.downcase!
case
when user_input.include? ("s")
user_input.gsub!(/s/, "th")
when user_input.include? ("ch")
user_input.gsub!(/ch/, "th")
when user_input == ""
puts "You typed noting! You get nothing sir!"
when user_input != ""
empty_string = false
else
puts "There are no 's's in your string."
end
end
puts "Zai jian, #{user_input}"
Below are the errors correlating by line and syntax error
rb.rb:9: syntax error, unexpected ( arg, expecting keyword_then or ',' or ';' or '\n'
when user_input.include? ("s")
rb.rb:11: syntax error, unexpected keyword_when, expecting keyword_end
when user_input.include? ("ch")
^
rb.rb:13: syntax error, unexpected keyword_when, expecting keyword_end
when user_input == ""
^
rb.rb:15: syntax error, unexpected keyword_when, expecting keyword_end
when user_input != ""
^
rb.rb:17: syntax error, unexpected keyword_else, expecting keyword_end
rb.rb:21: syntax error, unexpected keyword_end, expecting end-of-input
BELOW IS THE FIXED CODE THANKS TO #Phlip
empty_string = true
while empty_string do
print "Pleathe enter a thtring: "
user_input = gets.chomp
user_input.downcase!
case
when user_input.include?("s")
user_input.gsub!(/s/, "th")
empty_string = false
when user_input.include?("ch")
user_input.gsub!(/ch/, "th")
empty_string = false
when user_input == ""
puts "You typed noting! You get nothing sir!"
empty_string = true
else
puts "There are no 's's in your string."
end
end
puts "Zai jian, #{user_input}"
The issue was the spaces I had after .include?, #Phlip told me Ruby is space sensitive. I removed the white space and it worked. I ran into an issue with the boolean after and fixed that as well. It works as intended now.
My understanding is that you wish to ask the user for a string until the string contains "s" or "ch". When such a string is found you wish to make one or more substitutions in the string and print out a string containing the modified string. Here is a Ruby-like way of doing that.
user_input = nil
loop do
print "Pleathe enter a thtring: "
user_input = "cheater" # gets.chomp.downcase
case user_input
when /s/
user_input.gsub!('s','th')
break
when /ch/
user_input.gsub!('ch','th')
break
when ""
puts "You typed noting! You get nothing sir!"
else
puts "There are no 's's in your string."
end
end
puts "Zai jian, #{user_input}"
If the user enters an empty string, "You typed noting! You get nothing sir!" and then "Pleathe enter a thtring: " are displayed and gets awaits another entry.
If the user enters a non-empty string that contains no "s"'s or "ch"'s, "Pleathe enter a thtring: " is displayed and gets awaits another entry.
If the user enters "Chester\n" "Zai jian, chethter" is diplayed.
If the user enters "Cheater\N" "Zai jian, theater" is displayed.
If you actually wish to replace all "s"'s and "ch"'s, substitute the following for the first two when statements.
when /s|ch/
user_input.gsub!(/s|ch/,'th')
break
If this is done and user enters "Chester" "thethter" is displayed. (The when line could instead be written when /s/, /ch/, but I don't like that as well, in part because /s|ch/ is still needed as gsub!'s first argument.)
Note that case statements use the method Regexp#===. We therefore see that /s/.===(s) #=> true. Ruby allows us to write that /s/ === 'chester' ("syntactic sugar").
user_input = <anything> must precede the loop to make its value visible after the loop.
See Kernel#loop. For other uses of this method the handling of StopIteration exceptions is very useful when working with enumerators (instances of the Enumerator class).
=== looks a lot like ==, but they should be thought of as entirely different methods.
My assignment is to check whether a given input by the user contains the letters "c" or "s". I managed with one but, I simply don't know the correct way to write that.
I know that the problem is "s" || "c".
print 'What can we do for you?'
user_input = gets.chomp
user_input.downcase!
if user_input.empty?
puts 'Well you will have to write something...!'
elsif user_input.include? 's' || 'c'
puts "We got ourselves some 's's and some 'c's"
user_input.gsub!(/s/, 'th')
user_input.tr!('c', 's')
puts "The Daffy version, #{user_input}!"
else
print "Nope, no 's' or 'c' found"
end
simply
elsif user_input.include?("s") || user_input.include?("c")
or something like
%w(s c).any? { |command| user_input.include? command }
This is the perfect example of where regular expressions are fine:
user_input =~ /[sc]/
or:
(user_input.split('') & %w(s c)).any?
You can use Regexp
user_input[/s|c/]
No regexp:
user_input.count('sc') > 0
I was trying to re-prompt a user's input and reuse it. Here's the code sample:
print "Please put your string here!"
user_input = gets.chomp
user_input.downcase!
if user_input.include? "s"
user_input.gsub!(/s/,"th")
elsif user_input.include? ""
user_input = gets.chomp
puts "You didn't enter anything!Please type in something."
user_input = gets.chomp
else
print "no \"S\" in the string"
end
puts "transformed string: #{user_input}!"
My elsif will let the user know that their input was not acceptable, but was not effective in re-using their input to start from the beginning. How am I supposed to do it? Should I use a while or for loop?
Hope this solves your problem :)
while true
print 'Please put your string here!'
user_input = gets.strip.downcase
case user_input
when ''
next
when /s/
user_input.gsub!(/s/, "th")
puts "transformed string: #{user_input}!"
break
else
puts "no \"S\" in the string"
break
end
end
You can have a loop at the beginning to continuously ask for input until it's valid.
while user_input.include? "" #not sure what this condition is meant to be, but I took it from your if block
user_input = gets.chomp
user_input.downcase!
end
This will continuously ask for input until user_input.include? "" returns false. This way, you don't have to validate input later.
However, I'm not sure what you are trying to do here. If you want to re-prompt when the input is empty, you can just use the condition user_input == "".
EDIT: Here's the doc for String.include?. I tried running .include? "" and I get true for both empty and non-empty input. This means that this will always evaluate to true.
user_input = nil
loop do
print "Please put your string here!"
user_input = gets.chomp
break if user_input.length>0
end
user_input.downcase!
if user_input.include? "s"
user_input.gsub!(/s/,"th")
else
puts "no \"S\" in the string"
end
puts "transformed string: #{user_input}!"
I am creating a Daffy Duck speech converter (Very simple. Straight from CodeCademy) and I am having an issue with displaying the modified entry from the user.
Code:
puts "What would you like to convert to Daffy Duck language?"
user_input = gets.chomp
user_input.downcase!
if user_input.include? "s"
user_input.gsub!(/s/, "th")
print #{user_input}
else puts "I couldn't find any 's' in your entry. Please try again."
end
It will change any 's' in your entry to a 'th', therefore, making it sound like a Daffy Duck once read aloud. When I enter it into the interpreter, it will not display the modified string. It will just display the original entry by the user.
EDIT:
Thanks to the users below, the code is fixed, and I added a notice to the user with converted text. Thanks guys!
A # outside a string starts a comment, so #{user_input} is ignored, i.e.
print #{user_input}
is equivalent to
print
You might wonder why a single print outputs the original input. This is because without arguments print will print $_. That's a global variable which is set by gets:
user_input = gets.chomp # assume we enter "foo"
user_input #=> "foo"
$_ #=> "foo\n"
Everything works as expected if you pass a string literal:
print "#{user_input}"
or simply
print user_input
Note that gsub! returns nil if no substitutions were performed, so you can actually use it in your if statement:
if user_input.gsub!(/s/, "th")
print user_input
else
puts "I couldn't find any 's' in your entry. Please try again."
end
You just need to add double quotes around the string interpolation. Otherwise your code was just returning the input.
puts "What would you like to convert to Daffy Duck language?"
user_input = gets.chomp
user_input.downcase!
if user_input.include? "s"
user_input.gsub!(/s/, "th")
print "#{user_input}"
else
puts "I couldn't find any 's' in your entry. Please try again."
end
You don't even need interpolation, actually. print user_input works. Notice how StackOverflow was even syntax highlighting your code as a comment. :)