1

I'm able to work this statement to a point but I'd like to add another outcome. 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?

CSV File for example

def customer_check(user_pin)

  x = false

  CSV.read('customers.csv', headers: true).any? do |row|
    x = true if row['pin'] == user_pin and row['work_here'] == "YES"
    yellow = row['first_name']

  if x == true then
    puts "Welcome back #{yellow}."
    sleep(1.5)
  else
    puts "login failed. Please try again in 3 seconds..."
    sleep(3.0)
    login_start
  end
    navigation_menu
end
  • You can refactor `x = true if row['pin'] == user_pin && row['work_here'] == "YES"` to `x = row['pin'] == user_pin && row['work_here'] == "YES"` and second, `if x == true then` to `if x` only. – Jagdeep Singh Sep 30 '16 at 07:16
  • 2
    Isn't there an extra `end` keyword in above code? Can you please fix the indentation? It will be more clear then. – Jagdeep Singh Sep 30 '16 at 07:18
  • Sorry all I have just updated the code. Hope it makes more sense. @Jagdeep the changes you proposed didn't seem to work out but maybe because the code was incorrect. – Paul Thompson Sep 30 '16 at 09:39

2 Answers2

3

If you are 100% sure the pins in your csv are unique you could try this

def customer_check(user_pin)

  user = CSV.read('customers.csv', headers: true).detect { |row| 
      row['pin'] == user_pin && row['work_here'] == "YES"
  }

  if user
    puts "Welcome back #{user['first_name']}."
    sleep(1.5)
  else
    puts "login failed. Please try again in 3 seconds..."
    sleep(3.0)
    login_start
  end

  navigation_menu

end 

Variable user will be initialized as the corresponding row of the .csv for which the conditional is true. Otherwise it's value will be nil:

detect method according to documentation:

Passes each entry in enum to block. Returns the first for which block is not false. If no object matches, calls ifnone and returns its result when it is specified, or returns nil otherwise.

If no block is given, an enumerator is returned instead.

Bear in mind that you might have to ensure that row['pin'] matches your user_id data type as it may come in as a String in which case you'd have to row['pin'].to_i == user_pin

coolcheese
  • 106
  • 7
  • 1
    Another thing, I wouldn't really use `and` or `or` for logical operations in conditionals - [Difference between “and” and && in Ruby?](http://stackoverflow.com/questions/1426826/difference-between-and-and-in-ruby) – coolcheese Sep 30 '16 at 18:22
1

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

MmmHmm
  • 3,435
  • 2
  • 27
  • 49