# Extend an object with module stored

**URL:** <https://www.ruby-forum.com/t/extend-an-object-with-module-stored/99442>\
**Category:** Ruby\
**Created:** [January 31, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/extend-an-object-with-module-stored/99442 "2006-01-31T17:10:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![reis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f1d935/32.png) [@reis](https://www.ruby-forum.com/u/reis)\
**Post date:** [January 31, 2006, 5:10pm UTC](https://www.ruby-forum.com/t/extend-an-object-with-module-stored/99442/1 "2006-01-31T17:10:45Z")

</div>

I would like extend dynamically an object with one or more Module that I  
save into an Hash (I now only this way).

* * *

## categorie \<\< Categoria.new(‘Definizioni Testuali’, {:name\_module =\> ListaDefinizioni, }) categorie \<\< Categoria.new(‘Definizioni Testuali’, {:name\_module =\> ListaDefinizioni, :name\_module =\> ListaDefinizioniEditor})

Then I have store this information in a YAML file, but when I dump the  
hash I lost the info about the module.

* * *

— !ruby/array:Categorie

- !ruby/object:Categoria  
descrizione: Definizioni Testuali  
estensioni: # MY HASH  
:name\_module: !ruby/object:Module {}
- !ruby/object:Categoria  
descrizione: Definizioni Testuali  
estensioni: # MY HASH  
:name\_module: !ruby/object:Module {}

* * *

So when I try to extend, nothing to do.  
This code do not give me errors, but it do not extend my object.

* * *

# @categoria.extension is the hash with the module

## @categoria.estensioni.each\_value { |estensione| @denotato.send(‘extend’, estensione) }

Do you know some tecnique to extend an Object with module stored in YAML  
file?  
Thanks so much to all for the help.

–Reis

---

<div class="post-metadata">

**Author:** ![reis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f1d935/32.png) [@reis](https://www.ruby-forum.com/u/reis)\
**Post date:** [January 31, 2006, 8:12pm UTC](https://www.ruby-forum.com/t/extend-an-object-with-module-stored/99442/2 "2006-01-31T20:12:37Z")

</div>

Andrea R. wrote:

> ## Then I have store this information in a YAML file, but when I dump the estensioni: # MY HASH
> 
> Do you know some tecnique to extend an Object with module stored in YAML  
> file?  
> Thanks so much to all for the help.
> 
> –Reis

I had some code to serialize module and class identities (without any of  
their instance variables or class variables, though) with 1.8.2 (see  
below) but it seems not to work with 1.8.4. I will take a look at it  
later today.

class Module  
def is\_complex\_yaml?  
false  
end  
def to\_yaml( opts = {} )  
YAML::quick\_emit( nil, opts ) { |out|  
out \<\< "!ruby/module "  
self.name.to\_yaml( :Emitter =\> out )  
}  
end  
end  
YAML.add\_ruby\_type(/^module/) do |type, val|  
subtype, subclass = YAML.read\_type\_class(type, Module)  
val.split(/::/).inject(Object) { |p, n| p.const\_get(n)}  
end

class Class  
def to\_yaml( opts = {} )  
YAML::quick\_emit( nil, opts ) { |out|  
out \<\< "!ruby/class "  
self.name.to\_yaml( :Emitter =\> out )  
}  
end  
end  
YAML.add\_ruby\_type(/^class/) do |type, val|  
subtype, subclass = YAML.read\_type\_class(type, Class)  
val.split(/::/).inject(Object) { |p, n| p.const\_get(n)}  
end

---

<div class="post-metadata">

**Author:** ![reis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f1d935/32.png) [@reis](https://www.ruby-forum.com/u/reis)\
**Post date:** [January 31, 2006, 8:19pm UTC](https://www.ruby-forum.com/t/extend-an-object-with-module-stored/99442/3 "2006-01-31T20:19:50Z")

</div>

Andrea R. wrote:

> I would like extend dynamically an object with one or more Module that I  
> save into an Hash (I now only this way).
> 
> * * *
> 
> ## categorie \<\< Categoria.new(‘Definizioni Testuali’, {:name\_module =\> ListaDefinizioni, }) categorie \<\< Categoria.new(‘Definizioni Testuali’, {:name\_module =\> ListaDefinizioni, :name\_module =\> ListaDefinizioniEditor})
> 
> Then I have store this information in a YAML file, but when I dump the  
> hash I lost the info about the module.
> 
> \< snip YAML /\>
> 
> So when I try to extend, nothing to do.  
> This code do not give me errors, but it do not extend my object.
> 
> * * *
> 
> # @categoria.extension is the hash with the module
> 
> ## @categoria.estensioni.each\_value { |estensione| @denotato.send(‘extend’, estensione) }
> 
> Do you know some tecnique to extend an Object with module stored in YAML  
> file?  
> Thanks so much to all for the help.

I assume that the module used may vary from object to  
object (otherwise it is not necessary to to serialize  
it, of course)?

One simple thing you could do would be to store the  
_name_ of the module instead of the constant. Then,  
after loading the YAML, you can use Module#const\_get  
to obtain the constant again.

> –Reis

E

---

<div class="post-metadata">

**Author:** ![reis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f1d935/32.png) [@reis](https://www.ruby-forum.com/u/reis)\
**Post date:** [February 1, 2006, 4:54pm UTC](https://www.ruby-forum.com/t/extend-an-object-with-module-stored/99442/4 "2006-02-01T16:54:42Z")

</div>

Joel VanderWerf wrote:

> Joel VanderWerf wrote:
> 
> > I had some code to serialize module and class identities (without any of  
> > their instance variables or class variables, though) with 1.8.2 (see  
> > below) but it seems not to work with 1.8.4. I will take a look at it  
> > later today.
> 
> Here’s a version for 1.8.4. Are you listening, \_why, and is there a  
> chance of putting this in the YAML lib?

I will try in the next days.

> * * *
> 
> ## Sample code:
> 
> yy = [Enumerable, Comparable, String, File].to\_yaml  
> puts yy  
> p YAML.load(yy)
> 
> * * *
> 
> ## Output:
> 
> * * *
> 
> - !ruby/module Enumerable
> - !ruby/module Comparable
> - !ruby/class String
> - !ruby/class File  
> [Enumerable, Comparable, String, File]

Thanks for the answer.  
–Reis

---

<div class="post-metadata">

**Author:** ![reis](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f1d935/32.png) [@reis](https://www.ruby-forum.com/u/reis)\
**Post date:** [January 31, 2006, 8:49pm UTC](https://www.ruby-forum.com/t/extend-an-object-with-module-stored/99442/5 "2006-01-31T20:49:18Z")

</div>

Joel VanderWerf wrote:

> I had some code to serialize module and class identities (without any of  
> their instance variables or class variables, though) with 1.8.2 (see  
> below) but it seems not to work with 1.8.4. I will take a look at it  
> later today.

Here’s a version for 1.8.4. Are you listening, \_why, and is there a  
chance of putting this in the YAML lib?

* * *

## Sample code:

yy = [Enumerable, Comparable, String, File].to\_yaml  
puts yy  
p YAML.load(yy)

* * *

## Output:

* * *

- !ruby/module Enumerable
- !ruby/module Comparable
- !ruby/class String
- !ruby/class File  
[Enumerable, Comparable, String, File]

* * *

## Implementation:

require ‘yaml’

class Module  
yaml\_as “tag:ruby.yaml.org,2002:module”

def Module.yaml\_new( klass, tag, val )  
if String === val  
val.split(/::/).inject(Object) {|m, n| m.const\_get(n)}  
else  
raise YAML::TypeError, "Invalid Module: " + val.inspect  
end  
end

def to\_yaml( opts = {} )  
YAML::quick\_emit( nil, opts ) { |out|  
out.scalar( “tag:ruby.yaml.org,2002:module”, self.name, :plain )  
}  
end  
end

class Class  
yaml\_as “tag:ruby.yaml.org,2002:class”

def Class.yaml\_new( klass, tag, val )  
if String === val  
val.split(/::/).inject(Object) {|m, n| m.const\_get(n)}  
else  
raise YAML::TypeError, "Invalid Class: " + val.inspect  
end  
end

def to\_yaml( opts = {} )  
YAML::quick\_emit( nil, opts ) { |out|  
out.scalar( “tag:ruby.yaml.org,2002:class”, self.name, :plain )  
}  
end  
end
