Skip to content

Instantly share code, notes, and snippets.

@monkstone
Last active November 27, 2017 21:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save monkstone/70c8fd7ee6c0256f30200232d9c2ed79 to your computer and use it in GitHub Desktop.
Save monkstone/70c8fd7ee6c0256f30200232d9c2ed79 to your computer and use it in GitHub Desktop.
Reading and Writing YAML with jruby-complete.jar
---
bubbles:
- xpos: 20
ypos: 50
diameter: 200
emotion: happy
- xpos: 80
ypos: 60
diameter: 210
emotion: sad
colors:
- xpos: 20
ypos: 50
diameter: 200
colors: red
- xpos: 80
ypos: 60
diameter: 210
colors: blue
{"xpos"=>20, "ypos"=>50, "diameter"=>200, "emotion"=>"happy"}
{"xpos"=>80, "ypos"=>60, "diameter"=>210, "emotion"=>"sad"}
{"xpos"=>20, "ypos"=>50, "diameter"=>200, "colors"=>"red"}
{"xpos"=>80, "ypos"=>60, "diameter"=>210, "colors"=>"blue"}
require 'yaml'
data = YAML.load_file('data.yml')
data.fetch('bubbles').each do |bubble|
puts bubble
end
data.fetch('colors').each do |bubble|
puts bubble
end
require 'yaml'
values1 = [20, 50, 200, 'happy']
values2 = [80, 60, 210, 'sad']
labels = %w[xpos ypos diameter emotion]
labels2 = %w[xpos ypos diameter colors]
data = [values1, values2].map { |entry| labels.zip(entry).to_h }
data2 = [values1, values2].map { |entry| labels2.zip(entry).to_h }
hash = { 'bubbles' => data, 'colors' => data2 }
open('data.yml', 'w') { |file| file.write(hash.to_yaml) }
@monkstone
Copy link
Author

@jeremydouglass
Using jruby_complete:-

java -jar jruby-complete-9.1.13.0.jar write_yaml.rb

and

java -jar jruby-complete-9.1.13.0.jar read_yaml.rb

I have hand edited the data.yml produced by write_yaml.rb

@jeremydouglass
Copy link

Thank you! Extremely helpful example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment