簡體   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