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

  • Home
  • SEARCH
  • 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 258005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:08:40+00:00 2026-05-11T22:08:40+00:00

I am making a framework where objects should be created a according to a

  • 0

I am making a framework where objects should be created a according to a predefined XML file.
For example, if in the xml file the following occurs:

<type name="man"> 
    <property name="name" type="string">
    <property name="height" type="int">
    <property name="age" type="int">
    <property name="profession" type="string" value="unemployed">
</type> 

In Ruby, this should allow you to create an object as following:

man = Man.new('John', 188, 30)

Note: For fields where ‘value’ is defined in the xml, no value
should be accepted in the initialize method, but should rather be
set by the class itself as a default value.

Any recommended implementations for this?
I am current watching Dave Thomas’ screencasts about meta programming,
so this looks very suitable, but any suggestions would be appreciated!

  • 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-11T22:08:40+00:00Added an answer on May 11, 2026 at 10:08 pm

    Well, first you’ll need to parse the XML. You could use a library like Hpricot or Nokogiri for that. Here’s an example that will create a Man class given that type node from Nokogiri:

    def define_class_from_xml(node, in_module = Object)
      class_name = node['name'].dup
      class_name[0] = class_name[0].upcase
      new_class = in_module.const_set(class_name, Class.new)
    
      attributes = node.search('property').map {|child| child['name']}
      attribute_values = node.search('property[@value]').inject({}) do |hash, child|
        hash[child['name']] = child['value']
        hash
      end
    
      new_class.class_eval do
        attr_accessor *attributes
        define_method(:initialize) do |*args|
          needed_args_count = attributes.size - attribute_values.size
          if args.size < needed_args_count
            raise ArgumentError, "#{args.size} arguments given; #{needed_args_count} needed"
          end
          attributes.zip(args).each {|attr, val| send "#{attr}=", val}
          if args.size < attributes.size
            attributes[args.size..-1].each {|attr| send "#{attr}=", attribute_values[attr]}
          end
        end
      end
    end
    

    It’s not the most elegant bit of metaprogramming you’ll ever see, but I can’t think of how to make it any simpler at the moment. The first bit gets the class name and makes an empty class by that name, the second gets the attributes from the XML, and the third is the only real metaprogramming. It’s a class definition using that information (with the minor added hassle of needing to check the argument count, since we can’t tell Ruby “X number of arguments is required”).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 160k
  • Answers 160k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer When you create a UCM snapshot view, you reference the… May 12, 2026 at 11:38 am
  • Editorial Team
    Editorial Team added an answer In the context of Linq-to-SQL, an Entity basically just means… May 12, 2026 at 11:38 am
  • Editorial Team
    Editorial Team added an answer If you follow this example there's no reason it shouldn't… May 12, 2026 at 11:38 am

Related Questions

My unfamiliarity with the ASP.NET MVC framework and the plumbing thereof has brought me
I am trying to learn unit testing. I am trying to unit test some
I'm sure I must be doing something wrong. But can't for the life of
I want a strongly-typed DataSet along with designer TableAdapters, but Visual Studio's DataSet designer

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.