Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7025525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:59:10+00:00 2026-05-27T23:59:10+00:00

I am new here and I hope I will get answer to my question.

  • 0

I am new here and I hope I will get answer to my question.
I have three classes.

  1. Runner
  2. Writer
  3. Company

In runner class I have

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 is in class Company

write_entry is in class Writer

Following is the class Company code

  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

Following is the class Writer

 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

Kindly answer me the following questions:

  1. what will yeild entry(company) return (in company class)
  2. what will yield self return (in writer class)
  3. How can I write the node to xml file
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:59:11+00:00Added an answer on May 27, 2026 at 11:59 pm

    just look in what scope you are. Remember, self is implied :

    1. we are in an instance method, and it yields self.entry(company). So it will return the output of a call to entry(company) on the present instance ( wich seems to be ds in the first code you posted )

    2. still an instance method, so it will return self, a.k.a. the instanciated Writer on which fill is called, that is your writer object in the first code

    3. as @Frederick said, you should ask about the XML in another question and keep your question concise. Please read the FAQ to know more about how to use this site. Welcome to the community !

    EDIT : some help with the blocks

    blocks are somewhat like anonymous functions.

    whenever you do this :

    some_method {|block_args| some_block }
    

    just imagine you pass a function to some_method, and that function needs block_args. Now when you do :

    def some_method
      yield some_args
    end
    

    just imagine you call the block with block_args = some_args. It really helps to imagine a block like a simplified method signature, with no method name but with a list of arguments. In fact, this is like doing :

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

    with the notable exception that a block is bound to the context in which it is called:

     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
    

    now just imagine that yield is a function name that stands for the block ; if the block returns something, you can use it as the return of any other function. A little example with some dummy implementation of the already existing Enumerable#map method :

     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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to Facebook app development I hope I can get an answer here.
I'm new here so I hope I will write understandable question. My problem is,
I am new here and hope my first question meets stack overflow's requirements. I
I'm new here and I hope anyonte can help me. I have WCF Service
I'm new here and I hope I could get some help with an Android
I hope I get an answer to my question this time, I already posted
hello i am new here and i hope i don`t post in a wrong
I'm new here same as I'm new with assembly. I hope that you can
am new here. i have a slight problem; PLease look at the following code
I'm new here to stackoverflow, so bear with me. I have a book that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.