Syntax error in Ruby script [closed] - ruby

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

Related

Codeacademy - a night at the movies (ruby). Enabling the user to find movies without being case sensitive

I've just completed the Ruby CodeAcademy task, 'night at the movies' (code below).
When searching for existing movies in the database, I want the user to be able to input the title (in this case only StarWars or Divergent) without it having to be case sensitive? At the moment a search of 'starwars' returns an error (as opposed to StarWars which doesn't)!
Thanks
movies = {
StarWars: 4.8,
Divergent: 4.7
}
puts "What would you like to do? "
choice = gets.chomp
case choice
when "add"
puts "What movie would you like to add? "
title = gets.chomp.downcase
if movies[title.to_sym].nil?
puts "What rating does the movie have? "
rating = gets.chomp
movies[title.to_sym] = rating.to_i
else
puts "That movie already exists! Its rating is #{movies[title.to_sym]}."
end
when "update"
puts "Please enter movie title."
title = gets.chomp.to_sym
if movies[title].nil?
puts "This title doesn't exist!"
else
puts "what's the new rating for this movie (0 to 4)"
rating = gets.chomp
movies[title.to_sym] = rating.to_i
puts "#{title} has been updated with a rating of
#{rating}."
end
when "display"
movies.each do |movies, rating|
puts "#{movies}: #{rating}"
end
when "delete"
puts "Please enter movie title:"
title = gets.chomp.to_sym
if movies[title.to_sym].nil?
puts "Title doesn't exist"
else
movies.delete(title)
puts "#{title} has been deleted!"
end
else
puts "Error!"
end
You could use lowercase symbols
movies = { starwars: 4.8, divergent: 4.7}
and then turn the users input into all lowercase letters
title = gets.chomp.downcase.to_sym
this would turn any variance in capitalization of the titles (STARwars, StARwarS, ect...) into starwars

Multiple case/when conditions with || [duplicate]

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

Deleting a row from a CSV file with Ruby

I've seen similar answers to this question but I think I need something more specific to my code. Basically I've called the row from the CSV file but now I need to delete the called row. Sounds simple as I write this yet here I am asking you all for help. I know there is a lot of code here but I figured the more there is the more easier you will be able to understand the context. Apologies if there is too much noise in the code.
def delete_user_menu
puts "============================================"
delete_users_active_list
puts " "
puts "Please type in the name of the user you wish to eradicate: "
print "> "
eradicate(gets.chomp)
end
def eradicate(delete_input)
delete_row = delete_authentication(delete_input)
if delete_row
puts "Are you sure you want to delete #{delete_input} from the database?"
puts "[y]es or [n]o"
print "> "
delete_answer = gets.chomp
if delete_answer == "y"
delete_user
after_deletion_menu
elsif delete_answer == "n"
puts "Close call! Taking you back to main menu."
sleep 2
admin_main_menu
else
puts "Input not recognised. Please try again."
eradicate(delete_input)
end
else
puts "User not recognized. Please try again."
sleep 1
delete_user_menu
end
end
def delete_user
# this is where the delete user function needs to go
after_deletion_menu
end
def after_deletion_menu
puts " "
puts "User deleted! What would you like to do now?"
puts "1. Delete another user"
puts "2. Back to main menu"
print "> "
after_deletion_choice = gets.chomp
if after_deletion_choice == "1"
delete_user_menu
elsif after_deletion_choice == "2"
admin_main_menu
else
puts "Input not recognized. Please try again."
after_deletion_menu
end
end
def delete_users_active_list
CSV.foreach("./users.csv", headers: true) do |row|
username = row['username']
puts "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
puts "Username: #{username}"
end
end
def delete_authentication(username)
CSV.open('users.csv', headers: true).find { |row| row['username'] == username }
end
I've had a look at this question How to remove a row from a CSV with Ruby
but I don't fully understand the answers, hence why I'm here. Any help is much appreciated.
I looked at the link. First, they are reading the entire csv file into table:
table = CSV.table(#csvfile)
then deleting the row from table:
table.delete_if do |row|
row[:foo] == 'true'
end
Finally, they are completely replacing the original file with the new table minus the row:
File.open(#csvfile, 'w') do |f|
f.write(table.to_csv)
end
This is generally how you have to do this kind of operation when you are dealing with a file. It's not like a database.
EDIT - in your case:
delete_user(delete_input)
...
def delete_user(user)
...
table.delete_if { |row| row[:username] == user }
...

Rewriting an ATM program to require two files

I've created an ATM program and would like to rewrite certain aspects of it to require two files, one containing the names on the accounts and the money information, the other containing the pin number information here's the current code for the working ATM:
class Account
attr_reader :name, :checking_account, :savings_account, :transfer
def initialize(name, checking_account, savings_account)
#name = name
#checking_account = checking_account
#savings_account = savings_account
#transfer = transfer
end
def pin
#pin = '1234'
end
def display
puts "Welcome back #{name} enter your PIN:"
input = gets.chomp
if input == pin
main_menu
else
bad_pin
end
end
def main_menu
puts """
Welcome back #{name}!
Follow the instructions as follows:
To display Balance press '1'
To make a Withdrawl press '2'
To make a Deposit press '3'
To Transfer funds press '4'
To exit the system press '5'
"""
input = gets.chomp
case input
when '1'
balance
when '2'
withdrawl
when '3'
deposit
when '4'
transfer
else
exit
end
end
def balance
puts "Which balance? Checking or Savings?"
input = gets.chomp
if input =~ /checking/i
puts "Your balance for your Checking Account is: $#{checking_account}."
main_menu
elsif input =~ /savings/i
puts "Your balance for your Savings Account is: $#{savings_account}."
main_menu
else
main_menu
end
end
def withdrawl
puts "Please press '1' to make a withdrawl from checking. Press '2' to make a withdrawl from Savings. Press '3' to go back to the menu."
input = gets.chomp
case input
when '1'
puts "How much would you like to withdrawl?"
input = gets.chomp!
#checking_account -= input.to_f
puts "You have withdrawn $#{input}; you now have $#{checking_account} in your checking. Thank you for using this ATM you will be redirected to the main menu."
main_menu
when '2'
puts "How much would you like to withdraw?"
input = gets.chomp!
#savings_account -= input.to_f
puts "You have withdrawn ${input}; you now have $#{savings_account} in your savings. Thank you for using this ATM you will be redirected to the main menu"
main_menu
else
main_menu
end
end
def deposit
puts "Which account would you like to deposit into: Checkings(1), or Savings(2)?"
input = gets.chomp
if input == '1'
puts "Enter amount to deposit: "
amount = gets.chomp!
#checking_account += amount.to_f
puts "You have made a depost of $#{amount} leaving you with $#{checking_account}. Thank you for using this ATM. You will be redirected to the main menu."
main_menu
elsif input == '2'
puts "Enter amount to deposit: "
amount = gets.chomp!
#savings_account += amount.to_f
puts "You have made a deposit of $#{amount} leaving you with $#{savings_account}. Thank you for using this ATM. You will be redirected to the main menu."
main_menu
else
main_menu
end
end
def transfer
puts "Would you like to transfer funds from Checking(1) or Savings(2)?"
input = gets.chomp
if input == '1'
puts "Enter amount to transfer from Checking to Savings: Current balance #{checking_account}."
amount = gets.chomp
#checking_account -= amount.to_f
#savings_account += amount.to_f
puts "You have successfully transfered #{amount} from your Checking account new balance: #{checking_account}. Savings balance: #{savings_account}. You will be redirected to the main menu."
main_menu
elsif input == '2'
puts "Enter amount to transfer from Savings to Checking: Current balance #{savings_account}."
amount = gets.chomp
#savings_account -= amount.to_f
#checking_account += amount.to_f
puts "You have succesfully transfered #{amount} from your Savings account new balance: #{savings_account}. Checking account balance: #{checking_account}. You will be redirected to the main menu."
main_menu
else
puts "Invalid entry"
exit
end
end
def bad_pin
puts "Access Denied: incorrect PIN"
exit
end
end
my_account = Account.new("Thomas", 500_000, 750_000)
my_account.display
What I want to do is use two separate files, names.text and pins.text one holding the account names and the accounts information(names.txt), the other holding the pin numbers of the account(pins.txt).
names.text I.E.:
my_account = Account.new("Thomas", 500_000, 750_000)
my_account.display
my_account2 = Account.new("Jake", 50_000, 50_000)
my_account.display
my_account3 = Account.new("Anothername", 10, 50)
my_account.display
The file with the pin numbers(pins.txt) I think would look something like this(if not please enlighten me):
my_account #pin = 1234
my_account2 #pin = 2345
my_account3 #pin = 3456
So my questions are:
Where would I need to rewrite this code in order to acquire the information from the files, apart from the obvious pin method.
Would I need to use require or require_relative?

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 :-)

Resources