2015-11-02 56 views
-1

嗨即时编程Ruby的新手,我需要一点帮助。Ruby密钥和价值投入

我想在显示客户代码和客户名时进行检查。但我只获取客户代码而不是客户名称。我如何获得客户名?

customer = { 
C00001: "Tyler Dinges", 
C00002: "Jannie de Vries", 
C00003: "Klaas Bruinsma", 
} 

when 'searchcustomer' 
puts "Witch customer are you looking for?" 
customernumber = gets.chomp 
if customer[customernumber.to_sym].nil? 
    puts "customer is not found" 
else 
    customernumber == customer[customernumber.to_sym] 
    customer[customernumber.to_sym] 
    puts "#{customernumber} #{customername} Is a customer!" 
end 

感谢您的帮助!

+1

分配你需要做的'客户名称=顾客[customernumber.to_sym]'使得'customername'变量保存名称。 – SteveTurczyn

+0

通常'when'与'case'一起使用。你的'案件'在哪里? – shirakia

+0

这是我的代码的一小部分。 – Tylerdinges

回答

0

您需要删除关键字when

customer = { 
    C00001: "Tyler Dinges", 
    C00002: "Jannie de Vries", 
    C00003: "Klaas Bruinsma", 
} 


puts "Witch customer are you looking for?" 
customernumber = gets.chomp 
if customer[customernumber.to_sym].nil? 
    puts "customer is not found" 
else 
    customername = customer[customernumber.to_sym] 
    puts "#{customernumber} #{customername} Is a customer!" 
end 

我删除customernumber == customer[customernumber.to_sym],因为它已经被gets

+0

谢谢它的作品! 我需要的时候,因为我有一个案件。但是忘了太过分了吧。但这个作品男人谢谢! – Tylerdinges