# Creating an object from a pp string?

**URL:** <https://www.ruby-forum.com/t/creating-an-object-from-a-pp-string/158853>\
**Category:** Rails\
**Created:** [January 28, 2009, 8:27pm UTC](https://www.ruby-forum.com/t/creating-an-object-from-a-pp-string/158853 "2009-01-28T20:27:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![yannimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yannimac](https://www.ruby-forum.com/u/yannimac)\
**Post date:** [January 28, 2009, 8:27pm UTC](https://www.ruby-forum.com/t/creating-an-object-from-a-pp-string/158853/1 "2009-01-28T20:27:48Z")

</div>

When I use pp on an object in rails, it prints out a nice string  
representation of the object. I am trying to pass objects from one  
server to another and I am wondering if I can just pass this string and  
create an object on the other server. Any libraries/functions out there  
that can do this? Or any other way to do this (not necessarily using  
pp).

Thanks!

---

<div class="post-metadata">

**Author:** ![yannimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yannimac](https://www.ruby-forum.com/u/yannimac)\
**Post date:** [January 28, 2009, 8:34pm UTC](https://www.ruby-forum.com/t/creating-an-object-from-a-pp-string/158853/2 "2009-01-28T20:34:27Z")

</div>

> When I use pp on an object in rails, it prints out a nice string  
> representation of the object. I am trying to pass objects from one  
> server to another and I am wondering if I can just pass this string  
> and  
> create an object on the other server. Any libraries/functions out  
> there  
> that can do this? Or any other way to do this (not necessarily using  
> pp).

If you really want to go that route you might look at converting it to  
YAML… but I’d also suggest looking at JSON, XML, XML-RPC, REST,  
active resource, etc…

-philip

---

<div class="post-metadata">

**Author:** ![yannimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yannimac](https://www.ruby-forum.com/u/yannimac)\
**Post date:** [January 28, 2009, 8:40pm UTC](https://www.ruby-forum.com/t/creating-an-object-from-a-pp-string/158853/3 "2009-01-28T20:40:56Z")

</div>

Philip H. wrote:

> If you really want to go that route you might look at converting it to  
> YAML… but I’d also suggest looking at JSON, XML, XML-RPC, REST,  
> active resource, etc…
> 
> -philip

Thanks for the quick reply!

Right now I am just creating a pipe-delimited string of the object  
attributes. Then the other server takes that string, splits it on | and  
populates a new object. Really I am just wondering if there is a one  
liner from a library where I can do this. JSON, etc… would be  
overkill, since I can easily do it with a pipe-delimited string. I  
might just code a utility to do this with the pp string unless someone  
has some code that already does this. let me know… thanks

---

<div class="post-metadata">

**Author:** ![yannimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yannimac](https://www.ruby-forum.com/u/yannimac)\
**Post date:** [January 28, 2009, 8:46pm UTC](https://www.ruby-forum.com/t/creating-an-object-from-a-pp-string/158853/4 "2009-01-28T20:46:54Z")

</div>

> Right now I am just creating a pipe-delimited string of the object  
> attributes. Then the other server takes that string, splits it on |  
> and  
> populates a new object. Really I am just wondering if there is a one  
> liner from a library where I can do this. JSON, etc… would be  
> overkill, since I can easily do it with a pipe-delimited string. I  
> might just code a utility to do this with the pp string unless someone  
> has some code that already does this. let me know… thank

[http://www.ruby-doc.org/stdlib/libdoc/yaml/rdoc/classes/YAML.html#M010508](http://www.ruby-doc.org/stdlib/libdoc/yaml/rdoc/classes/YAML.html#M010508)  
[http://www.ruby-doc.org/stdlib/libdoc/yaml/rdoc/classes/YAML.html#M010509](http://www.ruby-doc.org/stdlib/libdoc/yaml/rdoc/classes/YAML.html#M010509)

---

<div class="post-metadata">

**Author:** ![yannimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yannimac](https://www.ruby-forum.com/u/yannimac)\
**Post date:** [January 28, 2009, 8:49pm UTC](https://www.ruby-forum.com/t/creating-an-object-from-a-pp-string/158853/5 "2009-01-28T20:49:05Z")

</div>

Philip H. wrote:

> > Right now I am just creating a pipe-delimited string of the object  
> > attributes. Then the other server takes that string, splits it on |  
> > and  
> > populates a new object. Really I am just wondering if there is a one  
> > liner from a library where I can do this. JSON, etc… would be  
> > overkill, since I can easily do it with a pipe-delimited string. I  
> > might just code a utility to do this with the pp string unless someone  
> > has some code that already does this. let me know… thank
> 
> [http://www.ruby-doc.org/stdlib/libdoc/yaml/rdoc/classes/YAML.html#M010508](http://www.ruby-doc.org/stdlib/libdoc/yaml/rdoc/classes/YAML.html#M010508)  
> [http://www.ruby-doc.org/stdlib/libdoc/yaml/rdoc/classes/YAML.html#M010509](http://www.ruby-doc.org/stdlib/libdoc/yaml/rdoc/classes/YAML.html#M010509)

Ah… you da man. Thanks, I guess I overlooked that 😉 That does  
exactly what I need it to.

---

<div class="post-metadata">

**Author:** ![yannimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yannimac](https://www.ruby-forum.com/u/yannimac)\
**Post date:** [January 28, 2009, 9:34pm UTC](https://www.ruby-forum.com/t/creating-an-object-from-a-pp-string/158853/6 "2009-01-28T21:34:32Z")

</div>

Here is the full implementation in case anyone is interested.

Server 2 needs to get a widget object from server 1

## Server 1 code

WidgetController  
def show\_widget\_yaml  
@widget = Widget.find(params[:id])  
render :layout =\> false  
end

show\_widget\_yaml.rhtml  
\<%=YAML.dump(@product)%\>

## Server 2 code

def show  
client = HTTPClient.new  
response =  
client.get('[http://www.server1/widgets/show\_widget\_yaml’+params[:id]](http://www.server1/widgets/show_widget_yaml'+params%5B:id%5D))  
yaml\_string = response.body.content  
@product = Product.new  
@product = YAML.load(yaml\_string)  
end

---

<div class="post-metadata">

**Author:** ![yannimac](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/y/7993a0/32.png) [@yannimac](https://www.ruby-forum.com/u/yannimac)\
**Post date:** [January 28, 2009, 9:37pm UTC](https://www.ruby-forum.com/t/creating-an-object-from-a-pp-string/158853/7 "2009-01-28T21:37:37Z")

</div>

Yanni M. wrote:

> Here is the full implementation in case anyone is interested.
> 
> Server 2 needs to get a widget object from server 1
> 
> ## Server 1 code
> 
> WidgetController  
> def show\_widget\_yaml  
> @widget = Widget.find(params[:id])  
> render :layout =\> false  
> end
> 
> show\_widget\_yaml.rhtml  
> \<%=YAML.dump(@product)%\>
> 
> ## Server 2 code
> 
> def show  
> client = HTTPClient.new  
> response =  
> client.get('[http://www.server1/widgets/show\_widget\_yaml’+params[:id]](http://www.server1/widgets/show_widget_yaml'+params%5B:id%5D))  
> yaml\_string = response.body.content  
> @product = Product.new  
> @product = YAML.load(yaml\_string)  
> end

Correction… I forgot to replace some variable names from my own code

## Server 1 code

WidgetController  
def show\_widget\_yaml  
@widget = Widget.find(params[:id])  
render :layout =\> false  
end

show\_widget\_yaml.rhtml  
\<%=YAML.dump(@widget)%\>

## Server 2 code

def show  
client = HTTPClient.new  
response =  
client.get('[http://www.server1/widgets/show\_widget\_yaml’+params[:id]](http://www.server1/widgets/show_widget_yaml'+params%5B:id%5D))  
yaml\_string = response.body.content  
@widget = Widget.new  
@widget = YAML.load(yaml\_string)  
end
