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 537495
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:55:58+00:00 2026-05-13T09:55:58+00:00

I need a Class which has an semi-automatic ‘to_s’ method (to generate XML in

  • 0

I need a Class which has an semi-automatic ‘to_s’ method (to generate XML in fact).
I would like to iterate through all the automatic methods set up in my ‘attr_accessor’ line:

class MyClass
    attr_accessor :id,:a,:b,:c
end

c=MyClass.new

So far I’m doing a basic:

c.methods - Object.methods

=> ["b", "b=", "c", "c=", "id=", "a", "a="]

I am facing a few challenges:

  1. ‘id’ may cause a slight headache – as Object already seems to have an ‘id’.
  2. The ‘c.methods’ call above, returns Strings – I’m not getting any other meta-data ? (In Java ‘method’ is an object, where I could perform further reflection).
  3. I have one-to-many relationships I have to deal with (‘c’ is an array type of other object types).

This is what I’m trying to do: I want to design a simple Object which has a ‘to_s’ which will build up an XML fragment: for instance.

<id> 1 </id>
<a> Title </a>
<b> Stuff </b>
<c>
    <x-from-other-object>
    <x-from-other-object>
    ....
</c>

And then inherit my data-classes from that simple object: so that (hopefully) I get a mechansim to build up an entire XML doc.

I’m sure I’m re-inventing the wheel here as well…so other tried-and-tested approaches welcome.

  • 1 1 Answer
  • 1 View
  • 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-13T09:55:59+00:00Added an answer on May 13, 2026 at 9:55 am

    To get method objects from a string, you can use the methods method or instance_method (where method would be called on an object and instance_method on a class). The only interesting information it gives you is arity, though (as opposed to java where it’d also give you the types of the return value and the arguments, which of course isn’t possible in ruby).

    Your title suggests that you only want to iterate over methods created by attr_accessor, but your code will iterate over every method defined in your class, which could become a problem if you wanted to add additional non-accessor methods to your class.

    To get rid of that problem and the problem with id, you could use your own wrapper around attr_accessor which stores which variables it created accessors for, like so:

    module MyAccessor
      def my_attr_accessor *attrs
        @attrs ||= []
        @attrs << attrs
        attr_accessor *attrs
      end
    
      def attrs
        @attrs
      end
    end
    
    class MyClass
      extend MyAccessor
      my_attr_accessor :id,:a,:b,:c
    
      def to_s
        MyClass.attrs.each do |attr|
          do_something_with(attr, send(attr))
        end
      end
    end
    

    For problem 3 you can just do

    if item.is_a? Array
      do_something
    else
      do_something_else
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 353k
  • Answers 353k
  • 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 Consider using regex with capturing groups. With Java's Matcher class,… May 14, 2026 at 7:46 am
  • Editorial Team
    Editorial Team added an answer If you're on SQL Server 2008 or 2008 R2, you… May 14, 2026 at 7:46 am
  • Editorial Team
    Editorial Team added an answer I'm running .NET 3.5 and Oracle 10gR2. I've added an… May 14, 2026 at 7:46 am

Related Questions

I have a class (simulation) which creates an instance of another class (GUI). Inside
My model contains a class Section which has an ordered list of Statics that
I wrote a small app that allows me to compare two database schemas to
I have UITableViewController as the RootViewController. I need to add rows to the table
Given that: 1) The C++03 standard does not address the existence of threads in

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.