# Exclude members from YAML conversion?

**URL:** <https://www.ruby-forum.com/t/exclude-members-from-yaml-conversion/154191>\
**Category:** Ruby\
**Created:** [November 26, 2008, 10:24am UTC](https://www.ruby-forum.com/t/exclude-members-from-yaml-conversion/154191 "2008-11-26T10:24:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [November 26, 2008, 10:24am UTC](https://www.ruby-forum.com/t/exclude-members-from-yaml-conversion/154191/1 "2008-11-26T10:24:13Z")

</div>

Hi!

I have the following class:

class user

attr\_accessor :userid, :username

def initialize  
@userid = “”  
@username = “”

@prefix = false  
@unprefix = false  
end

end

when i convert that with to\_yaml, all 4 member variables are translated  
into the yaml string. I need to exclude the members @prefix and  
@unprefix, they are used just for internal decisions and dosn’t need to  
be in the yaml string.

how do i do that?

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [November 26, 2008, 10:36am UTC](https://www.ruby-forum.com/t/exclude-members-from-yaml-conversion/154191/2 "2008-11-26T10:36:54Z")

</div>

Christian K. wrote:

> @username = “”  
> be in the yaml string.
> 
> how do i do that?

You can override yaml\_properties to return only the ones you want to be  
in the yaml string.

Cheers

–  
Ola B. ([http://olabini.com](http://olabini.com))  
JRuby Core Developer  
Developer, ThoughtWorks Studios ([http://studios.thoughtworks.com](http://studios.thoughtworks.com))  
Practical JRuby on Rails ([http://apress.com/book/view/9781590598818](http://apress.com/book/view/9781590598818))

“Yields falsehood when quined” yields falsehood when quined.

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [November 26, 2008, 10:49am UTC](https://www.ruby-forum.com/t/exclude-members-from-yaml-conversion/154191/3 "2008-11-26T10:49:35Z")

</div>

> You can override yaml\_properties to return only the ones you want to be  
> in the yaml string.

where do i overwrite this method?

in my user object?

can you give an example?

thx

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [November 26, 2008, 6:35pm UTC](https://www.ruby-forum.com/t/exclude-members-from-yaml-conversion/154191/4 "2008-11-26T18:35:57Z")

</div>

Christian K. wrote:

> > You can override yaml\_properties to return only the ones you want to be  
> > in the yaml string.
> 
> where do i overwrite this method?
> 
> in my user object?
> 
> can you give an example?
> 
> thx

require ‘yaml’

class C  
attr\_reader :x, :y, :z

def initialize x, y, z  
@x, @y, @z = x, y, z  
end

def to\_yaml\_properties  
super - ["@x"]  
end  
end

c = C.new 1, 2, 3  
p c.to\_yaml\_properties  
puts c.to\_yaml

**END**

Output:

["@y", “@z”]  
— !ruby/object:C  
y: 2  
z: 3

---

<div class="post-metadata">

**Author:** ![sealedsun](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/e68b1a/32.png) [@sealedsun](https://www.ruby-forum.com/u/sealedsun)\
**Post date:** [November 27, 2008, 8:01am UTC](https://www.ruby-forum.com/t/exclude-members-from-yaml-conversion/154191/5 "2008-11-27T08:01:57Z")

</div>

Joel VanderWerf wrote:

> require ‘yaml’  
> class C  
> attr\_reader :x, :y, :z
> 
> def initialize x, y, z  
> @x, @y, @z = x, y, z  
> end  
> def to\_yaml\_properties  
> super - ["@x"]  
> end  
> end  
> c = C.new 1, 2, 3  
> p c.to\_yaml\_properties  
> puts c.to\_yaml

Thx!
