开发者

Accessing objects in a ruby struct. Can find the object but cannot call method on the struct

开发者 https://www.devze.com 2023-02-03 14:36 出处:网络
class ScrapeJob < Struct.new(:scrape_id) def perform @scrape = Scrape.find(scrape_id) puts @scrape.link
class ScrapeJob < Struct.new(:scrape_id) 
    def perform
      @scrape = Scrape.find(scrape_id)
      puts @scrape.link
    end
end

Why can I find this object @scrape but I cannot ask it for any of it's attributes in this struct. @scrape.link is just a string and console returns an error that I cannot call a private me开发者_如何学编程thod. How can I get this objects info?


I think you are forgetting to give your Struct a name, so scrape_id becomes the name class name. The syntax you probably want to use is:

ScrapeJob = Struct.new(:scrape_id) do
   ...
end
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号