繁体   English   中英

如何让我的 API 在 Ruby 中返回多个值

[英]How to get my API to return more than one value in Ruby

我的 CLI 项目几乎正常工作,但是只返回一个值。 我希望我的程序返回多个值。

这是代码:

def display_info 
  puts "You'll love the following spots!"
  puts "********************************"
  @objects.each.with_index(1) {|brewery, index| puts "#{index}. #{brewery.name}"}  
  puts  "Please make a selection by index number for more information:"
  puts  "****************************************************"
  puts  "Type Quit to end. Type Menu to try another location."
  input = gets.strip.downcase
  if(input.to_i > 0)
    @brewery = @objects[input.to_i - 1] 
    puts "name: #{@brewery.name}"
    puts "street: #{@brewery.street}"
    puts "city: #{@brewery.city}"
    puts "phone: #{@brewery.phone}"
    puts "website_url: #{@brewery.website_url}"
    display_info
  elsif (input == "quit")
    quit 
  elsif (input == "menu")
    start
  else 
    puts "Ooops, please try again to get more info:"
    display_info
  end 
end

如果有帮助,这是输入结果。

    Austin
You'll love the following spots!
********************************
1. Oasis Texas Brewing Company
Please make a selection by index number for more information:
****************************************************
Type Quit to end. Type Menu to try another location.
quit
Goodbye. Drink responsibly and enjoy.

我该怎么做才能返回多个值?

这是这段代码的来源。 这是内容。

    class Breweries::API

  def self.get_breweries(input)
    @breweries_hash = HTTParty.get("https://api.openbrewerydb.org/breweries?by_city=#{input}")
    return if @breweries_hash.empty? #empty array handle 
    breweries_obj = {
      name: @breweries_hash[1]["name"],
      street: @breweries_hash[3]["street"],
      city: @breweries_hash[4]["city"],
      phone: @breweries_hash[10]["phone"],
      website_url: @breweries_hash[11]["website_url"]
    }
    Breweries::HoppyCode.new(breweries_obj)
  end
end 

end 

您需要使用带字符的gets拆分捕获的字符串。 你应该在你的消息中要求它,通常是一个逗号,可能被空格包围。

我用一个简化的脚本提取了问题,以便您可以单独测试它,这在解决问题时总是一个好主意。

注意在//分隔符之间使用正则表达式,后跟i标志以指示比较应该不区分大小写。

# what you would receive when you type this in the console following your message
# and captured by the gets, in this case brewerie 1 and 3
input = "1, 3" 
#split by a comma preceded or followed or not by a space
breweries = input.split(/ *, */) 
breweries.each do |brewerie|
  if(brewerie.to_i > 0)
    # @brewery = @objects[input.to_i - 1] 
    puts "displaying info about #{brewerie}"
  elsif brewerie[/quit/i]
    # quit
  elsif brewerie[/menu/i]
    # start
  else 
    puts "Ooops, please try again to get more info:"
    # display_info
  end 
end

返回:

displaying info about 1
displaying info about 3

据我了解,您要求用户从啤酒厂列表中选择一家啤酒厂。 当他们选择一个时,你向他们展示它的信息。 您可以做的是显示城市列表,让他们选择一个城市并选择城市匹配的@objects所有元素

def display_info 
  arr_of_cities = []
  @objects.each{|element| arr_of_cities.push(element.city)}
  arr_of_cities.each.with_index(1) {|city, index| puts "#{index}.#{city}"}  
  puts  "Please make a selection by index number for more information:"
  puts  "****************************************************"
  puts  "Type Quit to end. Type Menu to try another location."
  input = gets.strip.downcase
  arr_of_breweries = @objects.where(city:arr_of_cities[input.to_i- 1])
  if(input.to_i > 0)
    arr_of_breweries.each.with_index(1) do |brewery,index|
    puts "#{index}"
    puts "name: #{@brewery.name}"
    puts "street: #{@brewery.street}"
    puts "city: #{@brewery.city}"
    puts "phone: #{@brewery.phone}"
    puts "website_url: #{@brewery.website_url}"
   end
    display_info
  elsif (input == "quit")
    quit 
  elsif (input == "menu")
    start
  else 
    puts "Ooops, please try again to get more info:"
    display_info
  end 
end

希望这有帮助。 我有一段时间没有使用 ruby​​,所以我的语法可能有点偏差。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM