How to get the statement to return a value from the same row (headed 'first_name') in the CSV if the boolean statement returns true?
Running your code as is, Ruby expects a final "end" and raises this error:
syntax error, unexpected end-of-input, expecting keyword_end
Adding another ending 'end' to your code will result in the if x == true
block getting evaluated, but, note the indentation in the following code is arguably clearer. Using this example .csv file, this code will both print Welcome back...
and return
the first name if x == true
:
require 'csv'
def customer_check(user_pin)
x = false
CSV.read('customers.csv', headers: true).any? do |row|
x = true if row['customerNo'] == user_pin && row['lastName'] == "Dunbar"
yellow = row['firstName']
# The if/else clauses are indented within the CSV.read
if x # <-- "== true" is redundant
# puts "Welcome back #{yellow}." # you can lose "yellow" if you want to
puts "Welcome back #{row['firstName']}."
sleep(1.5)
# return yellow # <-- RETURN VALUE
return row['firstName'] # <-- RETURN VALUE
else
puts "login failed. Please try again in 3 seconds..."
sleep(3.0)
# login_start
end
# navigation_menu
end
end
return_name = customer_check('1') # <-- prints "Welcome back John"
puts return_name # <-- "John" is the RETURN VALUE
I'm not sure how you are using login_start
or navigation_menu
but i hope this helps to answer your question. If it helps, here's the CSV doc