Converting Strings to Symbols and Integers inside my hash in Ruby [closed] - ruby

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I made it this far, but don't know where to add the .to_i and the .to_sym methods since I'm assuming I put them in the wrong place. Can someone help me out? When I test the code out it says "undefined method `to_sym=' for nil:NilClass"
movies = {
spiderman: 3,
superman: 4,
batman: 5
}
puts "what movie do you like?"
choice = gets.chomp
case choice
when 'add'
puts "What movie do you want to add?"
title.to_sym = gets.chomp
puts "what is the rating of that movie?"
rating.to_i = gets.chomp
movies[title]=rating
puts "Added!"
when 'update'
puts "What movie would you like to update?"
title = gets.chomp
puts "Updated!"
when 'display'
movies.each do |movies, ratings|
puts "#{movies}: #{ratings}"
end
puts "Movies!"
when 'delete'
puts "What movie would you like to delete from the list?"
title = gets.chomp
puts "Deleted!"
else
puts "Error!"
end

Besides []=, there is no method of the form foo=, or to_sym=, as in your code, in plain Ruby. If you want to get a user input as a symbol, you can do:
title = gets.chomp.to_sym
If you want to get an integer, you can do:
rating = gets.to_i
I don't know why you had chomp in the latter case.

Related

Am I doing this right? If not could someone please explain how it's supposed to work? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
Hello i would like to know what wrong with my code
pizza = "2.99"
puts "hello what would you like to buy?"
food_type = gets.chomp.downcase
puts "How many?"
food_amount = gets.chomp!
puts #{food_amount.to_i * food_type.to_f}
To do something like this, you should store the different types of food in hash, and with { "food type" => cost of food } (for example, { 'pizza' => 9.99, 'soda' => 2.99, 'breadsticks' => 1.50 }) Note the prices are already floats, so no conversion is needed later.
Then when you loop through, you can save what they buy in another hash { 'food type' => quantity }, with the quantity being an integer. Then, when they finish selecting items, you loop through the 'cart' and multiply everything out:
menu = { 'pizza' => 9.99, 'soda' => 2.99, 'breadsticks' => 1.50 }
cart = Hash.new(0)
begin
puts "hello what would you like to buy?"
food_type = gets.chomp.downcase
puts "How many?"
quantity = gets.to_i
cart[food_type] += quantity
puts "Want Anything Else?"
buy_more = (gets.chomp == "yes")
end while buy_more
puts "you're buying:"
cart.each do |food_type, quantity|
puts "#{quantity} of #{food_type} for #{quantity * menu.fetch(food_type, 0)}"
end
Example Run:
hello what would you like to buy?
soda
How many?
2
Want Anything Else?
yes
hello what would you like to buy?
pizza
How many?
5
Want Anything Else?
yes
hello what would you like to buy?
breadsticks
How many?
1
Want Anything Else?
yes
hello what would you like to buy?
something else
How many?
10
Want Anything Else?
no
you're buying:
2 of soda for 5.98
5 of pizza for 49.95
1 of breadsticks for 1.5
10 of something else for 0
Read more about Hash in the docs, which should have enough info to get you going on the parts of your repl that I haven't added and decide how you'd like to handle invalid entries (like when I entered 'something else' for a food type). This example just treats it as a valid item with no price.

Ruby <, <=, >, >= value comparison code [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
The intent is to display message based on age.
puts "Age, please"
value = gets.chomp
if value < 21
puts "Here you cannot to buy alchohol"
end
puts "You can buy all the alchohol you want"
What is the missing part in this code?
if you get value by gets, value is String.
Use value.to_i
puts "Age, please"
value = gets
value = Integer(value) rescue 0
if value < 21
puts "Here you cannot to buy alchohol"
else
puts "You can buy all the alchohol you want"
end

Can I insert an `if` condition inside a `case` statement? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to build a game. I want to give the users options depending on what they choose. My code has to be in a case statement:
puts "HA! you are in Saw 18"
puts "Lets play a deadly game! >=D"
puts "Options: hight, Who is Superman, Bake a pie "
option = gets.chomp
case option
when "hight"
puts "Lets see your hight yo!"
option_1 = gets.chomp
case option_1
when
end
end
If they choose the Hight,
if i >= 5 ft
puts "you may live"
else
puts "you in trouble"
Any other alternatives using case are very welcome.
Yes, you can insert a condition inside the case statement.
You could call a function from the case statement and put the if condition inside the function. Unless you have a good reason not to do this?
Yes you can, I suppose (trying not to change your code too much) you could do something like this:
puts "HA! you are in Saw 18"
puts "Lets play a deadly game! >=D"
puts "Options are: height, Who is Superman or Bake a pie "
option = gets.chomp
case option
when "height"
puts "Lets see your height yo!"
option_1 = gets.chomp.to_i
if option_1 >= 5
puts "you may live"
else
puts "you're in trouble"
end
when "Who is Superman"
#code
else
#code
end

How to sort first before upcase-downcase in Ruby? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
This code is almost right, but I need to sort the inputs in alphabetical order before alternating between upcase and downcase.
football_team = []
5.times do |i|
puts "Please enter a UK football team:"
team = gets.chomp
if i.even?
football_team << team.upcase
else
football_team << team.downcase
end
end
puts football_team
I cannot use each_with_index.
I need to sort the inputs in alphabetical order before alternating between upcase and downcase.
I can identify 3 parts:
collect input
sort alphabetical
upcase and downcase
Obviously, this can't be done in a single loop.
That being said, here's one way to separate your code:
Part 1:
teams = []
5.times do |i|
puts "Please enter a UK football team:"
teams << gets.chomp
end
Part 2:
teams.sort!
Part 3:
5.times do |i|
if i.even?
teams[i].upcase!
else
teams[i].downcase!
end
end
puts teams
Adapting your answer, try this:
football_team = []
5.times do |i|
puts "Please enter a UK football team:"
team = gets.chomp.downcase
football_team << team
end
final_index = football_team.size - 1
football_team.sort!
(0..final_index).each do |i|
if i.even?
football_team[i] = football_team[i].upcase
else
football_team[i] = football_team[i].downcase
end
end
p football_team

Cannot get else condition to display [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I am working through a codeacademy project and I cannot get a certain condition to display. Here is my code.
When I select add and choose a movie to add, I want it to let me know if the movie I input already exists in the hash. When I type in a movie that already exists in the hash, it asks me for the rating anyway. I believe I have the else statement in the right place, but it does not seem to be working.
Update: I changed these two lines of code (removing the .to_sym)
title = title
if movies[title].nil?
It does not allow me to enter duplicates.
Now when I choose "add" then try to add "Memento" I get the error message
"#{title} already exists. Its rating is #{rating}!" #{rating} produces an integer of 1 (which makes no sense since the integer value is 4).
movies = {
"Memento" => 4,
"Inception" => 3,
"The Prestige" => 2,
"Interstellar" => 1
}
puts "What would you like to do?"
choice = gets.chomp.downcase
case choice
# ADD
when "add"
puts "What would you like to add?"
title = gets.chomp
title = title
if movies[title].nil?
puts "What its rating? (enter 1-4)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been added with a rating of #{rating}"
else puts "#{title} already exists. Its rating is #{rating}!"
end
# UPDATE
when "update"
puts "Updated!"
# DISPLAY
when "display"
puts "Movies!"
# DELETE
when "delete"
puts "Deleted!"
# ERROR
else
puts "Error!"
end
When you removed the to_sym calls you didn't remove all of them:
movies[title.to_sym] = rating.to_i
and
Its rating is #{rating}!"
doesn't reference the movie rating it references the variable rating. Which hasn't been set yet.
the else block should be
rating = movies[title]
puts "#{title} already exists. Its rating is #{rating}!"
Since title is a string, you don't need to convert it to a symbol. And then you can use Hash::include? to see if the key exists.
# ADD
when "add"
puts "What would you like to add?"
title = gets.chomp
# title = title.to_sym # delete this line since the hash keys are strings
if !movies.include? title # use Hash::include? to see if key exists
puts "What its rating? (enter 1-4)"
rating = gets.chomp
...
else
puts "Movie already exists. Its rating is #{movies[title]}!" # remove .to_sym
end

Resources