YAML and Class

Hi all,

I notice that the default YAML (ruby 1.8.1) does not dump or load Class by
default.
I have extended my local copy to do so, but now I wonder if there is a reason
why it does not support this feature that I have overlooked/not fully
appreciated?

Cheers,
Martin

FYI - this is my patch (Is there another way of creating a class without
having to eval the string?)

YAML.add_ruby_type(/^class/) { |type, val|
val =~ /\A[A-Z][A-Za-z0-9_](::[A-Z][A-Za-z0-9_])*\z/ or raise YAML::Error,
"Invalid Class: #{val.inspect}"
eval val
}

class Class
def to_yaml(opts = {})
YAML::quick_emit(nil, opts) { |out|
out << "!ruby/class "
self.name.to_yaml(:Emitter => out)
}
end
def is_complex_yaml?
false
end
end

Martin Hart wrote:

Hi all,

I notice that the default YAML (ruby 1.8.1) does not dump or load Class by
default.

nor does marshal:

 >> Marshal.dump( Class.new )
TypeError: can't dump anonymous class #<Class:0x81d2d14>
        from (irb):1:in `dump'
        from (irb):1

i can’t recall why this is, but the rule is there because I’ve adhered
to marshal’s rules. nahi or matz is better equipped to answer. i
thought we had this discussion on ruby-core, but i can’t seem to find
the reasoning.

_why

Hi,

···

In message “Re: YAML and Class” on 04/02/13, why the lucky stiff ruby-talk@whytheluckystiff.net writes:

nor does marshal:

>> Marshal.dump( Class.new )

TypeError: can’t dump anonymous class #Class:0x81d2d14
from (irb):1:in `dump’
from (irb):1

i can’t recall why this is, but the rule is there because I’ve adhered
to marshal’s rules. nahi or matz is better equipped to answer.

Just because anonymous class cannot be passed to another process.

						matz.

OK, thanks.

I’ll continue to use the tweak in my programs as I can ensure that receiving
process already has the class defined :slight_smile:

Cheers,
Martin

···

On Thursday 12 February 2004 17:35, Yukihiro Matsumoto wrote:

Hi,

i can’t recall why this is, but the rule is there because I’ve adhered
to marshal’s rules. nahi or matz is better equipped to answer.

Just because anonymous class cannot be passed to another process.

  					matz.