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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:34:33+00:00 2026-05-14T22:34:33+00:00

I’m trying to create a Messages object that inherits Array. The messages will gather

  • 0

I’m trying to create a Messages object that inherits Array. The messages will gather a group of Message objects. I’m trying to create an xml output with ROXML that looks like this:

<messages>
    <message>
        <type></type>
        <code></code>
        <body></body>
    </message>
    ...
</messages>

However, I can’t figure out how to get the message objects in the Messages object to display in the xml. Here is the code I’ve been working with:

require 'roxml'

class Message
  include ROXML

  xml_accessor :type
  xml_accessor :code
  xml_accessor :body
end

class Messages < Array
  include ROXML

  # I think this is the problem - but how do I tell ROXML that
  # the messages are in this instance of array?
  xml_accessor :messages, :as => [Message]

  def add(message)
    self << message
  end

end


message = Message.new
message.type = "error"
message.code = "1234"
message.body = "This is a test message."

messages = Messages.new
messages.add message

puts messages.length
puts messages.to_xml

This outputs:

1
<messages/>

So, the message object I added to messages isn’t getting displayed. Anyone have any ideas? Or am I going about this the wrong way?

Thanks for any help.

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

    I don’t think what you want is possible. You are somehow trying to get access to the internal state of the Array class, which is not only impossible because on most implementations those internals are hidden away in the C/C++/Java/.NET/Objective-C/ABAP runtime but also quite simply a bad idea and bad object-oriented design.

    The thing is, Messages is really not an Array, therefore it shouldn’t inherit from Array. Tell me: are you really 100% sure that your Messages class is able to faithfully fulfill the contracts of all 81 methods on Array? And what do assoc, rassoc, rindex and transpose even mean, when applied to Messages?

    You’d be much better off using delegation instead of inheritance here. This gives you a nice named entity that you can pass to xml_accessor:

    require 'forwardable'
    require 'roxml'
    
    class Messages
      extend Forwardable
      include ROXML
    
      class << self; alias_method :[], :new end
    
      xml_reader :messages, :as => [Message]
    
      def initialize(*messages) @messages = messages end
    
      def_delegators :messages, :length, :<<
    end
    

    Note: I also changed a couple of other things here. For example, I personally believe that an object should be valid and usable after it is constructed. In your version of the code, a Message is basically invalid after it is constructed and only becomes valid after you call the type=, code= and body= setters:

    class Message
      include ROXML
    
      class << self; alias_method :[], :new end
    
      xml_reader :type, :body
      xml_reader :code, :as => Integer
    
      def initialize(type=nil, code=nil, body=nil)
        @type, @code, @body = case opts = type
        when Hash
          opts[:type], opts[:code], opts[:body]
        else
          type, code, body
        end
      end
    end
    

    Here’s a slightly expanded usage example:

    msgs = Messages[Message['error', 1234, 'This is a test message.'], Message[]]
    
    msgs << Message[
      type: 'warning', 
      code: 4815162342, 
      body: 'This is another test message.'
    ]
    
    puts msgs.to_xml
    # => <messages>
    # =>   <message>
    # =>     <type>error</type>
    # =>     <body>This is a test message.</body>
    # =>     <code>1234</code>
    # =>   </message>
    # =>   <message/>
    # =>   <message>
    # =>     <type>warning</type>
    # =>     <body>This is another test message.</body>
    # =>     <code>4815162342</code>
    # =>   </message>
    # => </messages>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

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.