2011-12-28 16 views
0

我是新來的,我希望我能得到我的問題的答案。 我有三個班。Ruby中的產量以及塊如何工作

  1. 亞軍
  2. 作家
  3. 公司

在亞軍類我有

writer = Writer.new(directory + datasource.downcase + ".xml") 
ds = ("Sitemap::" + datasource).constantize.new(country_version, directory, country_host) 
writer.fill do 
      ds.yield_data {|entry| writer.write_entry(entry)} 
end 

Yeild_data是類公司

write_entry是類Writer

以下是類公司代碼

class Company 

    def initialize(country_version, directory, country_host) 
     @country_version = country_version 
     @directory = directory 
     @country_host = country_host 
    end 

    def yield_data 
     ::Company.find_each(:conditions => {:country_version_id => @country_version.id}) do |company| 
     output = yield entry(company) 
     puts output 
     end 
    end 

    private 
    def entry(company) 
     { 
     :url => ActionController::Integration::Session.new.url_for(:controller => 'companies', :action => 'show', :company_name => company.name, :host => @country_host.value), 
     :frequency => 0.8, 
     :priority => 'monthly', 
     :lastmod => company.updated_at 
     } 
    end 
    end 

以下是一流作家

class Writer 
    include ActionController::UrlWriter 

    def initialize(filepath) 
     @filepath = RAILS_ROOT + filepath 
     @xml_document = Nokogiri::XML::Document.new 
    end 

    def fill 
     File.open(@filepath,"w") do |f| 
     f.write(%[<?xml version="1.0" encoding="UTF-8"?>\n]) 
     f.write(%[<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">\n]) 
     yield self 
     f.write(%[</urlset>]) 
     f.close 
     end 
    end 

    def write_entry(entry) 
     node = Nokogiri::XML::Node.new("url", @xml_document) 
     node["loc"]= entry[:url] 
     node["lastmod"]= entry[:lastmod].to_s 
     node["changefreq"] = entry[:frequency].to_s 
     node["priority"] = entry[:priority].to_s 
     node.to_xml 
     #@filepath.write(node) 
    end 
    end 

請回答我以下的問題:

  1. 究竟會一代產量條目(公司)迴歸(在公司類)
  2. 什麼會產生自我回報(在作家類)
  3. 我怎麼能寫節點XML文件
+5

難道你只是*運行*代碼,看看它返回什麼? – 2011-12-28 11:28:03

+0

我運行的代碼,但我與輸出混淆? – wearybands 2011-12-28 12:09:23

+0

實際上在另一個塊內有塊,它讓我感到困惑 – wearybands 2011-12-28 12:10:20

回答

0

只是看在你是什麼樣的範圍。請記住,self暗示:

  1. 我們正處在一個實例方法,它產生self.entry(company)。因此,它會調用的輸出返回entry(company)在目前的情況下(至極似乎是在您發佈的第一個代碼ds

  2. 仍然是一個實例方法,那麼它將返回self,又名實例化Writerfill被稱爲,也就是你的writer對象在第一個代碼

  3. 作爲@Frederick說,你應該問在另一個問題的XML並保持你的問題簡潔。請閱讀FAQ以瞭解有關如何使用本網站的更多信息。歡迎來到社區!

編輯:一些幫助塊

塊是有點像匿名函數。

,只要你做到這一點:

some_method {|block_args| some_block } 

試想一下,你通過一個函數來some_method,並且功能需要block_args。現在,當你這樣做:

def some_method 
    yield some_args 
end 

試想一下,你調用塊block_args = some_args。想象一個像簡化方法簽名這樣的塊是非常有幫助的,它沒有方法名稱,但有一系列參數。其實,這就像做:

def some_method 
    my_block(some_args) 
end 

def my_block(block_args) 
    # do something and return the result 
end 

與顯着的例外,一個塊被綁定到調用它的背景:

class Myclass 
    def hi 
    # we are in an instance method, so self = the instance on which it is called 
    OtherClass.new.hello { self.name } 
    end 

    def name 
    "world" 
    end 
end 

class OtherClass 
    def hello 
    puts "hello #{ yield } !" # here, we call the block without args 
    end 
end 

# self.name is evaluated in the context it is called, so : 
>> Myclass.new.hi 
"hello world !" 
=> nil 

現在只是想象yield是一個函數名稱代表該塊;如果該塊返回某些內容,則可以將其用作任何其他函數的返回值。已有的Enumerable#map方法的一些虛擬實現的一個小例子:

def map 
    result = [] 
    self.each do |element| 
    result << yield element # store in an array the result of the block for each element... 
    end 
    result     # and return it 
end 

>> [1,2,3].map {|e| e * e } 
=> [1,4,9] 
+0

如果我們查看流水類中的塊...內塊返回所有的條目和外部塊(ds.yield_data {})都會返回一個類Company的實例?我是否正確?如果是的話我怎麼能用它來寫所有這些條目到我在填充方法中創建的xml文件? – wearybands 2011-12-28 14:40:42

+0

編號的內部塊('{| entry | writer.write_entry(entry)}')被提供來自'entry(company)'的結果,並將XML節點返回到'yield_data'方法中的'output'變量;然後顯示'puts',所以'yield_data'返回'nil'('puts'幾乎總是返回'nil')。外部塊有些沒用:你用'writer'實例來餵它,儘管你沒有在你的塊中使用這個實例+你的塊返回'nil' +你不使用'fill'中的內部塊的返回'方法,它只返回文件關閉的結果。 – 2011-12-28 15:28:21

+0

可以üPLZ糾正代碼寫入XML?我會感謝 – wearybands 2011-12-28 15:45:40

相關問題