You need to enable JavaScript to run this app. Ruby CLI应用中API获取的活动实例无法正常保存问题排查
优惠活动
大模型
产品
解决方案
定价
更多

Ruby CLI应用中API获取的活动实例无法正常保存问题排查

项目需求与现存问题
  • 功能目标:返回所有从API初始化得到的实例组成的数组,项目拆分三个核心模块:CLI交互模块、API请求模块、负责处理实例初始化的自定义类
  • 排查过程:通过binding.pry在CLI触发查看实例数组的操作前设置断点,此前触发查看逻辑时程序会直接返回空结果,调试时也确认存储实例的数组始终为空,暂时无法定位这些实例没有被正常保存的原因。

相关代码实现

CLI 类实现

class Cli

def run
    puts "Welcome to the I'm Bored app!"
    api = Api.new()
    input = nil
    while input != "exit"
        puts "How would you like to generate your event?"
        puts "You can generate by type, number of participants, or random!"
        puts "Types include education, recreational, social, diy, charity, cooking, relaxation, music, or busywork."
        puts "Participants can range from 0-5 number you like."
        puts "To generate a totally random event, type 'random'"
        puts "To see all events made so far, type 'events'."
        puts "To quit, type 'exit'."
            input = gets.strip.downcase

            if input == "type"
            puts
            puts "Please select education, recreational, social, diy, charity, cooking, relaxation, music, or busywork."
            input = gets.strip.downcase
            puts
            api.event_by_type(input)
            puts
            end
        

            if input == "participants"
            puts
            puts "Please enter your number of participants."
            input = gets.strip.downcase
            puts
            api.event_by_participants(input)
            puts
            end

            if input == "random"
            puts
            puts "Let's roll those dice!"
            puts
            api.event_by_random("activity")
            puts
            end

            if input == "events"
            puts
            puts "Here are your created events"
            puts
            #binding.pry
            Api.each do |key,value|       # map would work just as well, but not needed
                my_hash[key] = some_new_value    
              end
            # puts Api.all.each do |hash| 
            #     #hash.each do |key,value| 
            #       puts hash.type #(key + ': ' + value) 
            #     #end
              end
            puts
            # end
            # Api.all.map
            end
            
          if input == "exit"
            puts
            puts "Take care!"
            puts
            exit
        end     

        # if input != "types" && != "participants" && != "random" && != "events" && != "exit"
        #     input = gets.strip.downcase
        #     puts "I think you mispelled something. Try again."
        #     puts
        # end
    end
end

API 请求类实现

class Api
attr_accessor :url

@@all = []


def initialize()
    @url = "http://www.boredapi.com/api/" #does not work for some reason. returns empty. works fine with test in im-bored.???
end

 def self.all
     @@all
 end

def event_by_random(random)
    # http://www.boredapi.com/api/activity/
    req_url="#{@url}#{random}/"
    # 'puts req_url' # Test to make sure I still get the right URL
    data = HTTParty.get(req_url) 
    @@all << data
    puts data
end

def event_by_type(type)
    # http://www.boredapi.com/api/activity/
    req_url="#{@url}activity?type=#{type}"
    puts req_url # Test to make sure I still get the right URL
    data = HTTParty.get(req_url) 
    puts data
end

def event_by_participants(participants)
    # http://www.boredapi.com/api/activity/
    req_url="#{@url}activity?participants=#{participants}"
    # puts req_url # Test to make sure I still get the right URL
    data = HTTParty.get(req_url) 
    puts data
end
end

自定义 Suggestion 类实现

class Suggestion
 attr_accessor :type, :participants 
# attr_reader :activity, :price, :link, :key, :accessibility
@@all = []

  def initialize(type, participants)
      @type = type
      @participants = participants
      @@all << self
  end 

  def self.all
      @@all
  end 
end

核心困惑

代码运行过程中生成的活动实例始终没有被正常存入对应的存储数组,触发查看已生成活动的逻辑时无法获取到历史生成记录,暂时找不到问题的根本原因。

内容的提问来源于stack exchange,提问作者Pokedexrockett

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.04 20:06:03