1

好的,我正在尝试学习 ruby​​/rails(我在 1993 年开始学习 C++)

谁能告诉我这是为什么:

class FeedHolder
    @rssfeed
    @rss

    #attr_accessor :rssfeed,:rss

    def rssfeed
        @rssfeed
    end

    def initialize(feed,rss)
        @rssfeed,@rss = feed, rss
    end
end

像这样填充

  @rssfeeds.each do |rssfeed|
    @myrssurl=rssfeed.url.gsub(/'/,"")  
    @rss=Feedzirra::Feed.fetch_and_parse(@myrssurl) 
    @myfeedguy = FeedHolder.new(rssfeed,@rss)
    @myrsslist.push(@myfeedguy)

给我这个错误

“#FeedHolder:0xa0a8bec 的未定义方法 `rssfeed'”

当我这样做?顺便说一句,即使它没有 getter,它也不会抱怨对 myfeedguy.rss 的调用,是的,我尝试了 attr_accessor 和其他一些东西。

<% @myrsslist.each do |myfeedguy| %>
<% if(myfeedguy.rss == 0) then %>
        "No feeds to show"</br>
    <% else %>
        <%=myfeedguy.rssfeed%>

        </br>
            <a href='' onclick='showfeed("")'>x</a>
        </br>
    <% end %>
<%end%>

提前谢谢斯图尔特

4

1 回答 1

0

好像你在正确的轨道上。这个例子有帮助吗?

class FeedHolder
  def initialize(feed,rss)
    @rssfeed, @rss = feed, rss
  end
  attr_accessor :rssfeed,:rss
end

@rssfeeds = %w(a b c d)
@myrsslist = []

def fetch_and_parse(rssurl)
  "Imagine this was fetched and parsed from: #{rssurl}"
end

@rssfeeds.each do |rssfeed|  
  myrssurl = rssfeed.gsub(/'/,"")  
  rss = fetch_and_parse(myrssurl) 
  myfeedguy = FeedHolder.new(rssfeed, rss)
  @myrsslist.push(myfeedguy)
end

require 'pp'
pp @myrsslist

输出:

$ ruby tmp.rb 
[#<FeedHolder:0x00000001b00800
  @rss="Imagine this was fetched and parsed from: a",
  @rssfeed="a">,
 #<FeedHolder:0x00000001b00738
  @rss="Imagine this was fetched and parsed from: b",
  @rssfeed="b">,
 #<FeedHolder:0x00000001b00670
  @rss="Imagine this was fetched and parsed from: c",
  @rssfeed="c">,
 #<FeedHolder:0x00000001b005a8
  @rss="Imagine this was fetched and parsed from: d",
  @rssfeed="d">]
于 2012-09-27T14:52:43.477 回答