Multiple case/when conditions with || [duplicate] - ruby

This question already has answers here:
Case statement with multiple values in each 'when' block
(6 answers)
Closed 6 years ago.
I've been testing this code and it's not working as I expected. Can someone shed some light on this please ?
language = { JS: "Websites", Python: "Science", Ruby: "Web apps" }
puts "What language would you like to know? "
choice = gets.chomp
case choice
when "js" || "JS"
puts "Websites!"
when "Python" || "python"
puts "Science!"
when "Ruby" || "ruby"
puts "Web apps!"
else
puts "I don't know!"
end
, When I put in the first entry it runs, but if I use the latter entry it prints "I don't Know!"
i.e : if I enter 'js' runs, but If I enter 'JS' it throws 'I don't know!'

Please do search before asking question, you can get its answer easily in other questions
choice = gets.chomp
case choice
when 'js', 'JS'
puts 'Websites!'
when 'Python', 'python'
puts 'Science!'
when 'Ruby', 'ruby'
puts 'Web apps!'
else
puts "I don't know!"
end
After suggestions
choice = gets.chomp
puts case choice
when 'js', 'JS'
'Websites!'
when 'Python', 'python'
'Science!'
when 'Ruby', 'ruby'
'Web apps!'
else
"I don't know!"
end

Related

A/An articles in Ruby [duplicate]

This question already has answers here:
Rails article helper - "a" or "an"
(6 answers)
Closed 2 years ago.
Is there something like [#pluralize in ActiveSupport][1] but for a/an articles?
So basically I need something like:
'status'.articleize # => "a status"
'urgent'.articleize # => "an urgent"
It seems the Linguistics gem might be up for the job. Let's try Cary Swoveland's challenge:
require 'linguistics/en'
Linguistics.use(:en)
"one-eyed seaman".en.a
=> "a one-eyed seaman"
"honor".en.a
=> "an honor"
# And OP examples...
"urgent update".en.a
=> "an urgent update"
"status update".en.a
=> "a status update"
You could define String#articleize to return the appropriate English article:
class String
def articleize
if self[0] =~ /[aeiou]/i
"an #{self}"
else
"a #{self}"
end
end
end
'status'.articleize # => "a status"
'urgent'.articleize # => "an urgent"

Ruby conditional issue?

I have this code:
def name_of_client
puts "Hello sir/madam; please enter your name: "
name = gets.chomp.upcase
puts "Welcome to the Great Bank, #{name}. Would you like to enter your seriously insecure account? (Y/N)"
end
def get_response
answer = gets
if answer == "Y" || answer == "y"
puts 'Sure thing... '
elsif answer == "N" || answer== "n"
puts "Logging you out now. "
end
end
name_of_client
get_response
Why are the strings not printed when I type "Y" or "N"?
gets adds a new line to the answer variable. use gets.chomp or gets.strip instead. In future, I would recommend using the pry gem as shown below which pauses execution just like debugger does in javascript. To install pry open your terminal window (assuming you're running linux or macOS) and run gem install pry. You can then use it as shown below.
require "pry"
def get_response
answer = gets.strip
binding.pry
if answer == "Y" || answer == "y"
puts 'Sure thing... '
elsif answer == "N" || answer== "n"
puts "Logging you out now. "
end
end
get_response

Syntax error in Ruby script [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
Why am I getting a syntax error in the following code?
movies = { Transformers: 8,
Avengers: 10,
Loki: 7,
Chappie: 6
}
puts "__type add to add movie__"
puts "__type update to ping a movie__"
puts "__type rate to rate a movie__"
puts "__display to see all_"
choice=gets.chomp
case choice
when 'add'
puts "what movie would you like to watch?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "What is the rating"
rating=gets.chomp
movies[title.to_sym]=rating.to_i
puts "you will watch #{title} at #{rating} ratings"
else
puts "The #{title} movie has alraedy been added and its rating is #(Rating}"
end
when "update"
puts "what movie do you wanna update?"
update1=gets.chomp
if movies[update1.to_sym].nil?
puts "movie not found!"
else
puts "movie has been updated!"
end
when "display"
puts "movies"
end
when "delete"
puts "deleted"
else
puts "Error!"
end
You have an extra-end before when "delete".
end
when "delete"
Remove it. You should start indenting your code, so that it will be more evident the error.
movies = {
Transformers: 8,
Avengers: 10,
Loki: 7,
Chappie: 6
}
puts "__type add to add movie__"
puts "__type update to ping a movie__"
puts "__type rate to rate a movie__"
puts "__display to see all_"
choice=gets.chomp
case choice
when 'add'
puts "what movie would you like to watch?"
title = gets.chomp
if movies[title.to_sym].nil?
puts "What is the rating"
rating=gets.chomp
movies[title.to_sym]=rating.to_i
puts "you will watch #{title} at #{rating} ratings"
else
puts "The #{title} movie has alraedy been added and its rating is #(Rating}"
end
when "update"
puts "what movie do you wanna update?"
update1=gets.chomp
if movies[update1.to_sym].nil?
puts "movie not found!"
else
puts "movie has been updated!"
end
when "display"
puts "movies"
end # <-- here is the affected end
when "delete"
puts "deleted"
else
puts "Error!"
end

Ruby program doesn't save hash changes

My source code:
books = {
Harry_Potter: 5,
Steve_Jobs: 10
}
def finder(bookName)
books.each {
|n| if n == bookName
puts "Are you sure you want to #{choice} #{n}?"
confirmAction = gets.chomp
if confirmAction == "yes"
case choice
when "update"
puts "Enter the new name:"
newName = gets.chomp.to_sym
books[newName.to_sym] = books.delete(n)
puts "Update the rating for #{newName}:"
newRating = gets.chomp.to_i
books[newName.to_sym] = newRating.to_i
puts "Successfully updated #{newName} with rating of #{newRating}"
when "delete"
books.delete(n)
else puts "Invalid option!"
end
else puts "Invalid book name."
end
end
}
end
puts "What would you like to do?\n[Add] [Update] [Delete] [View]"
action = gets.chomp.capitalize
case action
when "Add"
puts "Enter the new book name:"
title = gets.chomp.to_sym
puts "Please rate the book [1-10]:"
rating = gets.chomp.to_i
books[title.to_sym] = rating.to_i
puts "Successfully added #{title} with rating of #{rating}"
puts books
when "Update"
choice = "update"
puts "Enter the name of the book:"
bookName = gets.chomp.to_sym
finder(bookName)
when "Delete"
choice = "delete"
puts "Enter the name of the book:"
bookName = gets.chomp.to_sym
finder(bookName)
when "View"
choice = "view"
puts books.each {
|k, v| puts "#{k}: #{v}"
}
end
Whenever I use add option and add something it works. But once I exit and re-open the program, it doesn't show books that I've added using the add option, it returns to the default list.
I need Ruby to save all the changes permanently.
You have to save your objects yourself, e.g. using YAML:
require 'yaml'
File.write('data.yml', YAML.dump(books))
The contents of "data.yml" will be:
---
:Harry_Potter: 5
:Steve_Jobs: 10
To read the file use:
books = YAML.load(File.read('data.yml'))
#=> {:Harry_Potter=>5, :Steve_Jobs=>10}
Well, you could use Maglev which is a ruby interpreter based on the GemStone/S Object Server which will be able to store your books persistently (by setting a reference to your books Hash and Maglev.commit_transaction). However this might be a bit of an overkill for your purposes :-)

Recursion in Ruby isnt working like I think it should

I don't understand why this method isn't working. when I put in a value that should pass the if statement it doesn't work.
def getBase
puts "What is the base URL for the test?"
x = gets
if (x.include? 'http://') && ((x.split('.').at(x.split('.').length - 1).length) == 3)
return x
else
puts "That is in the incorrect format."
puts "Please format your url like this"
puts "http://example.com"
getBase
end
end
input 'http://test.com'
result: statement repeats and does not exit recursion
When you get input with gets it includes the newline \n at the end (from the user hitting return). So your x is actually "http://test.com\n".
To get rid of this use String#chomp:
x = gets.chomp
That should do it.
If the purpose is to enforce correct URL format and/or make sure it's an HTTP URL, why don't you use a tool designed to do that? Ruby's URI class is your friend:
require 'uri'
URI.parse('http://foo.bar').is_a?(URI::HTTP)
=> true
URI.parse('ftp://foo.bar').is_a?(URI::HTTP)
=> false
URI.parse('file://foo.bar').is_a?(URI::HTTP)
=> false
URI.parse('foo.bar').is_a?(URI::HTTP)
=> false
I'd write the code more like this:
require 'uri'
def get_base
loop do
puts "What is the base URL for the test?"
x = gets.chomp
begin
uri = URI.parse(x)
return uri.to_s if uri.is_a?(URI::HTTP)
rescue URI::InvalidURIError
end
puts "That is in the incorrect format."
puts "Please format your URL like this:"
puts
puts " http://example.com"
end
end
puts "Got: #{ get_base() }"

Resources