Last active
August 29, 2015 14:17
-
-
Save retrography/87b4e25fdecf8307b627 to your computer and use it in GitHub Desktop.
Converts a Ruby object into a hash. This is not intended for object serialization or mapping, but instead for saving the information contained in an object to a document database, JSON file, etc...
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'yaml' | |
require 'json' | |
# The Ruby object called 'objie' contains information | |
# that need to be saved in a document database. 'objie' | |
# may or may not contain nested objects | |
yaml = objie.to_yaml | |
# Alternatively: yaml = YAML::load('objie.yml') | |
# Remove the Ruby object's YAML header | |
behead = yaml.lines[1..-1].join | |
# Remove the YAML header of each nested object | |
# while keeping the indentation intact | |
behead.gsub!(/^([- ]*)!ruby\/object:.*$\n\s*/,'\1') | |
behead.gsub!(/\s*!ruby\/object:.*$/,'') | |
# With all object headers removed, the remainder is a hash | |
hash = YAML::load(behead) | |
# Depending on the needs one may convert the hash into JSON | |
json = hash.to_json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: pry | |
version: | |
version: 0.10.1 | |
platform: ruby | |
authors: | |
- John Mair (banisterfiend) | |
- Conrad Irwin | |
- Ryan Fitzgerald | |
autorequire: | |
bindir: bin | |
cert_chain: [] | |
date: 2014-08-16 00:00:00.000000000 Z | |
dependencies: | |
- name: coderay | |
requirement: | |
requirements: | |
- - "~>" | |
- version: 1.1.0 | |
type: :runtime | |
prerelease: false | |
version_requirements: | |
requirements: | |
- - "~>" | |
- version: 1.1.0 | |
- name: slop | |
requirement: | |
requirements: | |
- - "~>" | |
- version: '3.4' | |
type: :runtime | |
prerelease: false | |
version_requirements: | |
requirements: | |
- - "~>" | |
- version: '3.4' | |
- name: method_source | |
requirement: | |
requirements: | |
- - "~>" | |
- version: 0.8.1 | |
type: :runtime | |
prerelease: false | |
version_requirements: | |
requirements: | |
- - "~>" | |
- version: 0.8.1 | |
- name: bundler | |
requirement: | |
requirements: | |
- - "~>" | |
- version: '1.0' | |
type: :development | |
prerelease: false | |
version_requirements: | |
requirements: | |
- - "~>" | |
- version: '1.0' | |
description: An IRB alternative and runtime developer console | |
email: | |
- [email protected] | |
- [email protected] | |
- [email protected] | |
executables: | |
- pry | |
extensions: [] | |
extra_rdoc_files: [] | |
files: | |
- CHANGELOG.md | |
- LICENSE | |
- README.md | |
- bin/pry | |
- lib/pry.rb | |
- lib/pry/cli.rb | |
- lib/pry/code.rb | |
- lib/pry/code/code_file.rb | |
- lib/pry/code/code_range.rb | |
- lib/pry/code/loc.rb | |
- lib/pry/code_object.rb | |
- lib/pry/color_printer.rb | |
- lib/pry/command.rb | |
- lib/pry/command_set.rb | |
- lib/pry/commands.rb | |
homepage: http://pryrepl.org | |
licenses: | |
- MIT | |
metadata: {} | |
post_install_message: | |
rdoc_options: [] | |
require_paths: | |
- lib | |
required_ruby_version: | |
requirements: | |
- - ">=" | |
- version: '0' | |
required_rubygems_version: | |
requirements: | |
- - ">=" | |
- version: '0' | |
requirements: [] | |
rubyforge_project: | |
rubygems_version: 2.2.2 | |
signing_key: | |
specification_version: 4 | |
summary: An IRB alternative and runtime developer console | |
test_files: [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name" => "pry", | |
"version" => { | |
"version" => "0.10.1" | |
}, | |
"platform" => "ruby", | |
"authors" => [ | |
[0] "John Mair (banisterfiend)", | |
[1] "Conrad Irwin", | |
[2] "Ryan Fitzgerald" | |
], | |
"autorequire" => nil, | |
"bindir" => "bin", | |
"cert_chain" => [], | |
"date" => 2014-08-16 00:00:00 UTC, | |
"dependencies" => [ | |
[0] { | |
"name" => "coderay", | |
"requirement" => { | |
"requirements" => [ | |
[0] [ | |
[0] "~>", | |
[1] { | |
"version" => "1.1.0" | |
} | |
] | |
] | |
}, | |
"type" => :runtime, | |
"prerelease" => false, | |
"version_requirements" => { | |
"requirements" => [ | |
[0] [ | |
[0] "~>", | |
[1] { | |
"version" => "1.1.0" | |
} | |
] | |
] | |
} | |
}, | |
[1] { | |
"name" => "slop", | |
"requirement" => { | |
"requirements" => [ | |
[0] [ | |
[0] "~>", | |
[1] { | |
"version" => "3.4" | |
} | |
] | |
] | |
}, | |
"type" => :runtime, | |
"prerelease" => false, | |
"version_requirements" => { | |
"requirements" => [ | |
[0] [ | |
[0] "~>", | |
[1] { | |
"version" => "3.4" | |
} | |
] | |
] | |
} | |
}, | |
[2] { | |
"name" => "method_source", | |
"requirement" => { | |
"requirements" => [ | |
[0] [ | |
[0] "~>", | |
[1] { | |
"version" => "0.8.1" | |
} | |
] | |
] | |
}, | |
"type" => :runtime, | |
"prerelease" => false, | |
"version_requirements" => { | |
"requirements" => [ | |
[0] [ | |
[0] "~>", | |
[1] { | |
"version" => "0.8.1" | |
} | |
] | |
] | |
} | |
}, | |
[3] { | |
"name" => "bundler", | |
"requirement" => { | |
"requirements" => [ | |
[0] [ | |
[0] "~>", | |
[1] { | |
"version" => "1.0" | |
} | |
] | |
] | |
}, | |
"type" => :development, | |
"prerelease" => false, | |
"version_requirements" => { | |
"requirements" => [ | |
[0] [ | |
[0] "~>", | |
[1] { | |
"version" => "1.0" | |
} | |
] | |
] | |
} | |
} | |
], | |
"description" => "An IRB alternative and runtime developer console", | |
"email" => [ | |
[0] "[email protected]", | |
[1] "[email protected]", | |
[2] "[email protected]" | |
], | |
"executables" => [ | |
[0] "pry" | |
], | |
"extensions" => [], | |
"extra_rdoc_files" => [], | |
"files" => [ | |
[ 0] "CHANGELOG.md", | |
[ 1] "LICENSE", | |
[ 2] "README.md", | |
[ 3] "bin/pry", | |
[ 4] "lib/pry.rb", | |
[ 5] "lib/pry/cli.rb", | |
[ 6] "lib/pry/code.rb", | |
[ 7] "lib/pry/code/code_file.rb", | |
[ 8] "lib/pry/code/code_range.rb", | |
[ 9] "lib/pry/code/loc.rb", | |
[ 10] "lib/pry/code_object.rb", | |
[ 11] "lib/pry/color_printer.rb", | |
[ 12] "lib/pry/command.rb", | |
[ 13] "lib/pry/command_set.rb", | |
[ 14] "lib/pry/commands.rb", | |
], | |
"homepage" => "http://pryrepl.org", | |
"licenses" => [ | |
[0] "MIT" | |
], | |
"metadata" => {}, | |
"post_install_message" => nil, | |
"rdoc_options" => [], | |
"require_paths" => [ | |
[0] "lib" | |
], | |
"required_ruby_version" => { | |
"requirements" => [ | |
[0] [ | |
[0] ">=", | |
[1] { | |
"version" => "0" | |
} | |
] | |
] | |
}, | |
"required_rubygems_version" => { | |
"requirements" => [ | |
[0] [ | |
[0] ">=", | |
[1] { | |
"version" => "0" | |
} | |
] | |
] | |
}, | |
"requirements" => [], | |
"rubyforge_project" => nil, | |
"rubygems_version" => "2.2.2", | |
"signing_key" => nil, | |
"specification_version" => 4, | |
"summary" => "An IRB alternative and runtime developer console", | |
"test_files" => [] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "pry", | |
"version": { | |
"version": "0.10.1" | |
}, | |
"platform": "ruby", | |
"authors": [ | |
"John Mair (banisterfiend)", | |
"Conrad Irwin", | |
"Ryan Fitzgerald" | |
], | |
"autorequire": null, | |
"bindir": "bin", | |
"cert_chain": [ | |
], | |
"date": "2014-08-16 00:00:00 UTC", | |
"dependencies": [ | |
{ | |
"name": "coderay", | |
"requirement": { | |
"requirements": [ | |
[ | |
"~>", | |
{ | |
"version": "1.1.0" | |
} | |
] | |
] | |
}, | |
"type": "runtime", | |
"prerelease": false, | |
"version_requirements": { | |
"requirements": [ | |
[ | |
"~>", | |
{ | |
"version": "1.1.0" | |
} | |
] | |
] | |
} | |
}, | |
{ | |
"name": "slop", | |
"requirement": { | |
"requirements": [ | |
[ | |
"~>", | |
{ | |
"version": "3.4" | |
} | |
] | |
] | |
}, | |
"type": "runtime", | |
"prerelease": false, | |
"version_requirements": { | |
"requirements": [ | |
[ | |
"~>", | |
{ | |
"version": "3.4" | |
} | |
] | |
] | |
} | |
}, | |
{ | |
"name": "method_source", | |
"requirement": { | |
"requirements": [ | |
[ | |
"~>", | |
{ | |
"version": "0.8.1" | |
} | |
] | |
] | |
}, | |
"type": "runtime", | |
"prerelease": false, | |
"version_requirements": { | |
"requirements": [ | |
[ | |
"~>", | |
{ | |
"version": "0.8.1" | |
} | |
] | |
] | |
} | |
}, | |
{ | |
"name": "bundler", | |
"requirement": { | |
"requirements": [ | |
[ | |
"~>", | |
{ | |
"version": "1.0" | |
} | |
] | |
] | |
}, | |
"type": "development", | |
"prerelease": false, | |
"version_requirements": { | |
"requirements": [ | |
[ | |
"~>", | |
{ | |
"version": "1.0" | |
} | |
] | |
] | |
} | |
} | |
], | |
"description": "An IRB alternative and runtime developer console", | |
"email": [ | |
"[email protected]", | |
"[email protected]", | |
"[email protected]" | |
], | |
"executables": [ | |
"pry" | |
], | |
"extensions": [ | |
], | |
"extra_rdoc_files": [ | |
], | |
"files": [ | |
"CHANGELOG.md", | |
"LICENSE", | |
"README.md", | |
"bin/pry", | |
"lib/pry.rb", | |
"lib/pry/cli.rb", | |
"lib/pry/code.rb", | |
"lib/pry/code/code_file.rb", | |
"lib/pry/code/code_range.rb", | |
"lib/pry/code/loc.rb", | |
"lib/pry/code_object.rb", | |
"lib/pry/color_printer.rb", | |
"lib/pry/command.rb", | |
"lib/pry/command_set.rb", | |
"lib/pry/commands.rb", | |
], | |
"homepage": "http://pryrepl.org", | |
"licenses": [ | |
"MIT" | |
], | |
"metadata": { | |
}, | |
"post_install_message": null, | |
"rdoc_options": [ | |
], | |
"require_paths": [ | |
"lib" | |
], | |
"required_ruby_version": { | |
"requirements": [ | |
[ | |
">=", | |
{ | |
"version": "0" | |
} | |
] | |
] | |
}, | |
"required_rubygems_version": { | |
"requirements": [ | |
[ | |
">=", | |
{ | |
"version": "0" | |
} | |
] | |
] | |
}, | |
"requirements": [ | |
], | |
"rubyforge_project": null, | |
"rubygems_version": "2.2.2", | |
"signing_key": null, | |
"specification_version": 4, | |
"summary": "An IRB alternative and runtime developer console", | |
"test_files": [ | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- !ruby/object:Gem::Specification | |
name: pry | |
version: !ruby/object:Gem::Version | |
version: 0.10.1 | |
platform: ruby | |
authors: | |
- John Mair (banisterfiend) | |
- Conrad Irwin | |
- Ryan Fitzgerald | |
autorequire: | |
bindir: bin | |
cert_chain: [] | |
date: 2014-08-16 00:00:00.000000000 Z | |
dependencies: | |
- !ruby/object:Gem::Dependency | |
name: coderay | |
requirement: !ruby/object:Gem::Requirement | |
requirements: | |
- - "~>" | |
- !ruby/object:Gem::Version | |
version: 1.1.0 | |
type: :runtime | |
prerelease: false | |
version_requirements: !ruby/object:Gem::Requirement | |
requirements: | |
- - "~>" | |
- !ruby/object:Gem::Version | |
version: 1.1.0 | |
- !ruby/object:Gem::Dependency | |
name: slop | |
requirement: !ruby/object:Gem::Requirement | |
requirements: | |
- - "~>" | |
- !ruby/object:Gem::Version | |
version: '3.4' | |
type: :runtime | |
prerelease: false | |
version_requirements: !ruby/object:Gem::Requirement | |
requirements: | |
- - "~>" | |
- !ruby/object:Gem::Version | |
version: '3.4' | |
- !ruby/object:Gem::Dependency | |
name: method_source | |
requirement: !ruby/object:Gem::Requirement | |
requirements: | |
- - "~>" | |
- !ruby/object:Gem::Version | |
version: 0.8.1 | |
type: :runtime | |
prerelease: false | |
version_requirements: !ruby/object:Gem::Requirement | |
requirements: | |
- - "~>" | |
- !ruby/object:Gem::Version | |
version: 0.8.1 | |
- !ruby/object:Gem::Dependency | |
name: bundler | |
requirement: !ruby/object:Gem::Requirement | |
requirements: | |
- - "~>" | |
- !ruby/object:Gem::Version | |
version: '1.0' | |
type: :development | |
prerelease: false | |
version_requirements: !ruby/object:Gem::Requirement | |
requirements: | |
- - "~>" | |
- !ruby/object:Gem::Version | |
version: '1.0' | |
description: An IRB alternative and runtime developer console | |
email: | |
- [email protected] | |
- [email protected] | |
- [email protected] | |
executables: | |
- pry | |
extensions: [] | |
extra_rdoc_files: [] | |
files: | |
- CHANGELOG.md | |
- LICENSE | |
- README.md | |
- bin/pry | |
- lib/pry.rb | |
- lib/pry/cli.rb | |
- lib/pry/code.rb | |
- lib/pry/code/code_file.rb | |
- lib/pry/code/code_range.rb | |
- lib/pry/code/loc.rb | |
- lib/pry/code_object.rb | |
- lib/pry/color_printer.rb | |
- lib/pry/command.rb | |
- lib/pry/command_set.rb | |
- lib/pry/commands.rb | |
homepage: http://pryrepl.org | |
licenses: | |
- MIT | |
metadata: {} | |
post_install_message: | |
rdoc_options: [] | |
require_paths: | |
- lib | |
required_ruby_version: !ruby/object:Gem::Requirement | |
requirements: | |
- - ">=" | |
- !ruby/object:Gem::Version | |
version: '0' | |
required_rubygems_version: !ruby/object:Gem::Requirement | |
requirements: | |
- - ">=" | |
- !ruby/object:Gem::Version | |
version: '0' | |
requirements: [] | |
rubyforge_project: | |
rubygems_version: 2.2.2 | |
signing_key: | |
specification_version: 4 | |
summary: An IRB alternative and runtime developer console | |
test_files: [] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment