ruby 将Ruby对象转换为哈希。这不是用于对象序列化或映射,而是用于保存包含的信息

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 将Ruby对象转换为哈希。这不是用于对象序列化或映射,而是用于保存包含的信息相关的知识,希望对你有一定的参考价值。

--- !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:
- jrmair@gmail.com
- conrad.irwin@gmail.com
- rwfitzge@gmail.com
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: []
{
  "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": [
    "jrmair@gmail.com",
    "conrad.irwin@gmail.com",
    "rwfitzge@gmail.com"
  ],
  "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": [

  ]
}
{
                         "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] "jrmair@gmail.com",
        [1] "conrad.irwin@gmail.com",
        [2] "rwfitzge@gmail.com"
    ],
                  "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" => []
}
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:
- jrmair@gmail.com
- conrad.irwin@gmail.com
- rwfitzge@gmail.com
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: []
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

以上是关于ruby 将Ruby对象转换为哈希。这不是用于对象序列化或映射,而是用于保存包含的信息的主要内容,如果未能解决你的问题,请参考以下文章

如何将 JSON 转换为 Ruby 哈希

如何表示将 ruby​​ 哈希转换为 yaml 的 aws 内部函数

Rails:如何将对象数组的哈希转换为 json

Ruby // 使用输入从哈希中选择和传输项目

如何将带有对象作为键的 Ruby 哈希对象作为 Map 对象发送到前端 Javascript

将 json 格式的键值对转换为以符号为键的 ruby​​ 哈希的最佳方法是啥?