Working with Ruby by Jan Lelis

Small Ruby CLI Improvements (Part 4): Edit Ruby Objects Using yaml

Somehow, I stumbled upon this useful little script by _why: Update

Lovely, Simple, Prismatic I say it’s prismatic because this little snippy marries a few of my true loves together. Allow users to edit Ruby objects in YAML using your editor of choice.

 1
2
3
4
5
6
7
8
9
10
11
12
13
# why the luck stiff's "object aorta"
# http://rubyforge.org/snippet/detail.php?type=snippet&id=22

require 'yaml'
def aorta( obj )
  tempfile = File.join('/tmp',"yobj_#{ Time.now.to_i }")
  File.open( tempfile, 'w' ) { |f| f << obj.to_yaml }
  system( "#{ ENV['EDITOR'] || 'vi' } #{ tempfile }" )
  return obj unless File.exists?( tempfile )
  content = YAML::load( File.open( tempfile ) )
  File.delete( tempfile )
  content
end

Update: I’ve added this functionality to the interactive_editor gem, which is – thanks to github – now available with version 0.0.7 :D

[2,3,4].vim #=> [2,3,42]

Creative Commons License

Comments

scragz | March 10, 2011

I miss _why ><;

mkorfmann | March 11, 2011

Me too :( . But fractions of his work, like this one, never let him die :) .

andy | March 15, 2011

This looks cool!
Tried running [1,2,3].vim in IRB and got the error below. Just running 'vim' from the IRB prompt works ok.
Using Ubuntu / Ruby 1.8.7
TypeError: can't convert Tempfile into String
from /home/andyl/.gems/ruby/1.8/gems/interactive_editor-0.0.7/lib/interactive_editor.rb:35:in `initialize'
from /home/andyl/.gems/ruby/1.8/gems/interactive_editor-0.0.7/lib/interactive_editor.rb:35:in `open'
from /home/andyl/.gems/ruby/1.8/gems/interactive_editor-0.0.7/lib/interactive_editor.rb:35:in `edit'
from /home/andyl/.gems/ruby/1.8/gems/interactive_editor-0.0.7/lib/interactive_editor.rb:59:in `edit'
from /home/andyl/.gems/ruby/1.8/gems/interactive_editor-0.0.7/lib/interactive_editor.rb:96:in `vim'
from (irb):6

J-_-L | March 18, 2011

Oops, I'll fix that. Btw, no problems with 1.9 ;)

J-_-L | March 28, 2011

0.0.8 fixes the 1.8 issue

Mary Ellen Kennedy Can I do my wedding over again? It was 21 years ago and my puirtecs were stale by comparison. These photos brought tears to my eyes. What a perfect way to capture an unforgettable day. | July 01, 2015

Mary Ellen Kennedy Can I do my wedding over again? It was 21 years ago and my puirtecs were stale by comparison. These photos brought tears to my eyes. What a perfect way to capture an unforgettable day.

Just ate at Jui Thai Asian Cafe at 787 Bethel Road (the former <a href="http://xevabzld.com">lotocian</a> of Lilly's) and they had a very interesting menu with none of the typical lunch special dishes. I had the Delicious Pork Cake ($3, very tasty) and the Hot Pot ($6) which had noodles, bok choi, napa, luncheon meat, quail egg, bean skin, and fish ball. The hot pot was saltier and more oily than I anticipated, but was delicious all the same. I plan to return with my family next week to try more dishes. | July 02, 2015

Just ate at Jui Thai Asian Cafe at 787 Bethel Road (the former <a href="http://xevabzld.com">lotocian</a> of Lilly's) and they had a very interesting menu with none of the typical lunch special dishes. I had the Delicious Pork Cake ($3, very tasty) and the Hot Pot ($6) which had noodles, bok choi, napa, luncheon meat, quail egg, bean skin, and fish ball. The hot pot was saltier and more oily than I anticipated, but was delicious all the same. I plan to return with my family next week to try more dishes.

That's an apt answer to an inineesttrg question http://kjjedbvd.com [url=http://ggxcaf.com]ggxcaf[/url] [link=http://uqrjhyfsxnc.com]uqrjhyfsxnc[/link] | July 02, 2015

That's an apt answer to an inineesttrg question http://kjjedbvd.com [url=http://ggxcaf.com]ggxcaf[/url] [link=http://uqrjhyfsxnc.com]uqrjhyfsxnc[/link]

Have you tried Borgata Pizza Cafe on Parkville Street? It is definitely off the <a href="http://fwyudc.com">betean</a> path and is locally owned. They make their own spaghetti noodles, meatballs, sauce, etc. I don't know of too many places that actually make their own pasta. Their New York Style pizza is great too. | July 04, 2015

Have you tried Borgata Pizza Cafe on Parkville Street? It is definitely off the <a href="http://fwyudc.com">betean</a> path and is locally owned. They make their own spaghetti noodles, meatballs, sauce, etc. I don't know of too many places that actually make their own pasta. Their New York Style pizza is great too.

I second New Chopsticks in Dublin. I'm Chinese American oraginilly from New York City, and this is the first place I found that had decent Gong Chow Naw Haw. http://onwflykgioa.com [url=http://aqrdozvtd.com]aqrdozvtd[/url] [link=http://erlndp.com]erlndp[/link] | July 06, 2015

I second New Chopsticks in Dublin. I'm Chinese American oraginilly from New York City, and this is the first place I found that had decent Gong Chow Naw Haw. http://onwflykgioa.com [url=http://aqrdozvtd.com]aqrdozvtd[/url] [link=http://erlndp.com]erlndp[/link]

You? | July 30, 2015