Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parsing of XML return a singleton Hash that is not dumpable #35

Closed
HoneyryderChuck opened this issue Jan 27, 2012 · 4 comments
Closed

Comments

@HoneyryderChuck
Copy link

I'm using the parsing engine in order to get hash representation that will be serialized somewhere. In order for it to be serialized, the object cannot be singleton. Turns out, what the parsing function returns is indeed singleton, therefore, not dumpable.

quick example:



xml = '
This
is
freaky
'

Marshal.dump Crack::XML.parse(xml)



this returns: TypeError: singleton can't be dumped

Is it short-term-fixable?

Regards
Tiago

@HoneyryderChuck
Copy link
Author

Hi,

investigated further, and it seems it is all because of these lines:


if t.is_a?(String)
        class << t
          attr_accessor :attributes
        end
        t.attributes = attributes
      end

line 85 - 90 of the xml.rb file.

Apparently, after the insertion of the attributes accessor on the t variable, t is no longer an instance of String, but a singleton instance(?) of String. Not dumpable, therefore. How to fix this? I'll investigate further, just wanted to tell u about so u could know of the issue as well.

@HoneyryderChuck
Copy link
Author

The main culprit of this issue has been (and now a quote from the Marshal class documentation for Ruby 1.x.x):

Some objects cannot be dumped: if the objects to be dumped include bindings, procedure or method objects, instances of class IO, or singleton objects, a TypeError will be raised.

the solutions, as far as i see it is, are:

  • either do it all with instance_variable_get/set methods when it comes to accessing the attributes,
  • or create a subclass that inherits from String, define the attributes accessor inside and move on from there.

what do you think?

@mhuggins
Copy link

Anyone have a solution or workaround for this in the past 7 months?

@kiskoza
Copy link
Collaborator

kiskoza commented Jan 26, 2024

Seems to be fixed already

@kiskoza kiskoza closed this as completed Jan 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants