我尝试创建的 Ruby 方法有问题

Posted

技术标签:

【中文标题】我尝试创建的 Ruby 方法有问题【英文标题】:I have an issue with my Ruby method I am trying to create 【发布时间】:2017-02-23 02:51:40 【问题描述】:

我正在尝试执行以下代码,但没有得到我想要的响应。我想在客户详细信息哈希中设置孩子的答案。我很确定这是因为我的 children 方法中有一个 get.chomp,但我不确定我是否可以将该响应设置为一个整数,以便我可以将它铲到我的哈希中。

# Get following details from client:
# name
# age
# number of children
# decor theme
# handicap
# if handicap ask if they would like wheelchair ramp or elevator.
# Program should also:
# Print the hash back to screen when designer answers all questions
# Give the user the option to update a key. Exit if user says "n." If      user enters "key," program should ask for new key value and update key.   (Strings have methods to turn them into symbols.)
# Print latest version of hash, exit


def children(response)
 until response == "y" || response == "n"
      puts "Invalid response, Please enter Y/N only!"
      response = gets.chomp.downcase
     end
      if response == "y"
        puts "How many children do you have?"
        number_of_children = gets.chomp.to_i
       else response == "n"
        number_of_children = 0
       end
end




client_details = 

# ---- Driver Code ----

puts  "Welcome to the Client Information Form!"

# ---- get the user name ----
puts "Please enter the Clients Full Name:"
client_details[:name] = gets.chomp
# ---- get the user age ----
puts "Please enter #client_details[:name] age:"
client_details[:age] = gets.chomp.to_i
# ---- find out if the user has children ----
puts "Does #client_details[:name] have any children? (Please enter Y/N only)"
children_input = gets.chomp.downcase.to_s
children(children_input)



client_details[:children] = children(children_input)
p client_details

这是我运行代码时发生的情况:

'Welcome to the Client Information Form!
Please enter the Clients Full Name:
Art V
Please enter Art V age:
55
Does Art V have any children? (Please enter Y/N only)
y
How many children do you have?
8
How many children do you have?
8
:name=>"Art V", :age=>55, :children=>8

【问题讨论】:

向我们展示您遇到的错误,这将有助于我们诊断问题并提出解决方案。没有它,我们所能做的就是猜测。 同时,您会遇到此语句else number_of_children == "n" 语法不遵循的问题。 "else" 应该是独立的,没有条件,或者你可以使用 if CONDITION ~ elsif CONDITION ~ else DEFAULTACTION。见link IF-ELSIF-ELSE @CaptainChaos 这不是一个错误我很抱歉,它只是问了两次问题: ---------- 欢迎使用客户信息表!请输入客户全名:Art V 请输入 Art V 年龄:55 Art V 有孩子吗? (请仅输入 Y/N) y 您有几个孩子? 8 你有几个孩子? 8 :name=>"Art V", :age=>55, :children=>8 @halfer 你能提供任何帮助吗? 是的,我明白你的意思,不是错误,而是意外响应。下一次,明确说明问题(您多次收到“多少个孩子……”的问题) 【参考方案1】:

你得到重复输出的原因是你调用了两次children方法:

# this is the first time here
children_input = gets.chomp.downcase.to_s
children(children_input)

# and this is the second time here
client_details[:children] = children(children_input)

如果您只想调用一次,则需要保存/存储返回值,例如:

children_input = gets.chomp.downcase.to_s
num_children = children(children_input)
client_details[:children] = num_children

【讨论】:

以上是关于我尝试创建的 Ruby 方法有问题的主要内容,如果未能解决你的问题,请参考以下文章

在 Slim 中使用 Ruby on Rails 有条件地创建隐藏属性

在 Ruby 中从多维数组创建排列

在 Ruby 中从多维数组创建排列

如何使用 ruby​​ api 创建一个 bigquery 表并从云存储导入

Ruby祖先链中的方法访问有啥问题

在方法中调用方法以在 Ruby 中进行 Titleize