本文介绍了Ruby 项目帮助.无法从数组中获取已保存的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TLDR 我正在开发一个使用 API 创建实例的项目.我希望能够回忆起我的所有实例,但不知道如何回忆.我对 Ruby 和一般编程比较陌生,所以我希望我能很好地解释一切.这是我的代码.

TLDR I am working on a project that creates instances using an API. I want to be able to recall all of my instances but can't figure out how. I'm relatively new to Ruby and programming in general so I hope I'm explaining everything well enough. Here's my code.

class Suggestion
    attr_accessor :type, :participants 
    attr_reader :activity, :price, :link, :key, :accessibility

    @@all = []

    def initialize(type, participants)
        @type = type
        @participants = participants
        # @activity = []
        # @price = price
        # @key = key
        # @accessibility = accessibility
         @@all << self
    end

    # def save
    #     @@all << self
    # end

    def self.all
        @@all
    end

    # def events
    #     @@all.map
    # end
    
    def list_events
        # binding.pry
        Suggestion.all.map #{|event| [:activity, :type, :participants, :price, :link, :key, :accessibility]}
    end

end

任何和所有帮助将不胜感激

any and all help would be greatly appreciated

推荐答案

考虑下面的类定义,我把 @all 变成了一个 类实例变量,而不是比类变量.使用前者通常优于使用后者.

Consider the following class definition, where I've made @all a class instance variable, rather than a class variable. Use of the former is generally preferred over use of the latter.

class Suggestion
  attr_accessor :type, :participants

  @all = []

  class << self
    attr_accessor :all
  end

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

  def list_events
    self.class.all.map do |instance|
      { type: instance.type, participants: instance.participants }
    end 
  end

  def self.list_events
    all.map do |instance|
      { type: instance.type, participants: instance.participants }
    end 
  end
end

片段

class << self
  attr_accessor :all
end

@all 创建一个读写访问器.类< 导致 self 更改为 Suggestion 的单例类.如果需要,您可以将其替换为

creates a read-write accessor for @all. class << self causes self to be changed to Suggestion's singleton class. If desired you could replace this with

superclass.public_send(:attr_accessor, :all)


让我们试试吧.


Let's try it.

Suggestion.all
  #=> []
s1 = Suggestion.new(1, ['Bob', 'Sally'])
  #=> #<Suggestion:0x00007fc677084888 @type=1, @participants=["Bob", "Sally"]>
Suggestion.all
  #=> [#<Suggestion:0x00007fc677084888 @type=1, @participants=["Bob", "Sally"]>]
s2 = Suggestion.new(2, ['Ronda', 'Cliff'])
  #<Suggestion:0x00007fc677b577b0 @type=2, @participants=["Ronda", "Cliff"]>
Suggestion.all
  #=> [#<Suggestion:0x00007fc677084888 @type=1, @participants=["Bob", "Sally"]>,
  #    #<Suggestion:0x00007fc677b577b0 @type=2, @participants=["Ronda", "Cliff"]>]
s3 = Suggestion.new(3, ['Weasel', 'Isobel'])
  #=> #<Suggestion:0x00007fc677077598 @type=3, @participants=["Weasel", "Isobel"]>
Suggestion.all
  #=> [#<Suggestion:0x00007fc677084888 @type=1, @participants=["Bob", "Sally"]>,
  #    #<Suggestion:0x00007fc677b577b0 @type=2, @participants=["Ronda", "Cliff"]>,
  #    #<Suggestion:0x00007fc677077598 @type=3, @participants=["Weasel", "Isobel"]>]
s1.list_events
  #=> [{:type=>1, :participants=>["Bob", "Sally"]},
  #    {:type=>2, :participants=>["Ronda", "Cliff"]},
  #    {:type=>3, :participants=>["Weasel", "Isobel"]}]
Suggestion.list_events
  #=> [{:type=>1, :participants=>["Bob", "Sally"]},
  #    {:type=>2, :participants=>["Ronda", "Cliff"]},
  #    {:type=>3, :participants=>["Weasel", "Isobel"]}]

我包含了 Suggestion#instance 来展示实例如何访问(读取或写入)类实例变量 @all.

I included Suggestion#instance to show how an instance can access (read or write) the class instance variable @all.

这篇关于Ruby 项目帮助.无法从数组中获取已保存的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 00:52