简体   繁体   English

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

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

My CLI project is working almost correctly, however, is only returning one value.我的 CLI 项目几乎正常工作,但是只返回一个值。 I would like my program to return multiple values.我希望我的程序返回多个值。

Here's the code:这是代码:

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

Here's the input result if that helps.如果有帮助,这是输入结果。

    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.

What can I do to return more than one value?我该怎么做才能返回多个值?

Here's the source of this code.这是这段代码的来源。 This is the content.这是内容。

    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 

You need to split the string you capture using gets with a character.您需要使用带字符的gets拆分捕获的字符串。 You should ask for it in your message, normally that would be a comma, possibly surrounded by spaces.你应该在你的消息中要求它,通常是一个逗号,可能被空格包围。

I extracted the problem in a simplified script so that you can test it separately, which is always a good idea when solving a problem.我用一个简化的脚本提取了问题,以便您可以单独测试它,这在解决问题时总是一个好主意。

Mind the use of regular expressions between the // delimiters, and followed by the i flag to indicate that the comparison should be case-insensitive.注意在//分隔符之间使用正则表达式,后跟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

Which returns:返回:

displaying info about 1
displaying info about 3

From what I understand, You are asking the user to pick a brewery from a list of breweries.据我了解,您要求用户从啤酒厂列表中选择一家啤酒厂。 When they pick one, you show its info to them.当他们选择一个时,你向他们展示它的信息。 What you could do instead is display a list of cities and let them pick a city and select all elements of the @objects where the city matches您可以做的是显示城市列表,让他们选择一个城市并选择城市匹配的@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

Hopefully that helps.希望这有帮助。 I haven't used ruby in a while so my syntax might be off a bit.我有一段时间没有使用 ruby​​,所以我的语法可能有点偏差。

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

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