[Ruby Study Notes] 24.Ruby JSON and RubyGems
[Ruby Study Notes] 24.Ruby JSON and RubyGems
Preface
This chapter introduces Ruby’s JSON and RubyGems.
Ruby JSON
In this chapter, we will introduce how to use Ruby language to encode and decode JSON objects.
Environment configuration
Before using Ruby to encode or decode JSON data, we need to install the Ruby JSON module. You need to install Ruby gem before installing this module. We use Ruby gem to install the JSON module. However, if you are using the latest version of Ruby, you may have the gem installed. After parsing it, we can use the following command to install the Ruby JSON module:
$gem install [json](/search?q=json)
Use Ruby to parse JSON.
The following is JSON data and store the data in the input.json file:
input.json file
{
"President": "Alan Isaac",
"CEO": "David Richardson",
"India": [
"Sachin Tendulkar",
"Virender Sehwag",
"Gautam Gambhir"
],
"Srilanka": [
"Lasith Malinga",
"Angelo Mathews",
"Kumar Sangakkara"
],
"England": [
"Alastair Cook",
"Jonathan Trott",
"Kevin Pietersen"
]
}
![\[Ruby Study Notes\] 24.Ruby JSON and RubyGems](6b44e99974d17195ee2722671aabdc1f.png)
The following Ruby program is used to parse the above JSON file;
Example
#!/usr/bin/[ruby](/search?q=ruby)
require 'rubygems'
require 'json'
require 'pp'
json = File.read('input.json')
obj = JSON.parse(json)
pp obj
The execution result of the above example is:
{"President"=>"Alan Isaac",
"CEO"=>"David Richardson",
"India"=>
["Sachin Tendulkar", "Virender Sehwag", "Gautam Gambhir"],
"Srilanka"=>
["Lasith Malinga ", "Angelo Mathews", "Kumar Sangakkara"],
"England"=>
["Alastair Cook", "Jonathan Trott", "Kevin Pietersen"]
}
Ruby RubyGems
RubyGems is a package manager for Ruby that provides a standard format for distributing Ruby programs and libraries, as well as a tool for managing package installation.
RubyGems is a tool designed to easily manage gem installations, as well as a server for distributing gems. This is similar to apt-get under Ubuntu, yum in Centos, and pip in Python.
RubyGems was created around November 2003 and has been part of the Ruby standard library since Ruby version 1.9.
If your Ruby version is lower than 1.9, you can also install it manually:
First download the installation package: https://rubygems.org/pages/download .
Unzip and enter the directory, execute the command: ruby setup.rb
update RubyGems command:
$ gem update --system # 需要管理员或root用户
Gem
Gem is a package manager for Ruby modules (called Gems). It contains package information, as well as files used for installation.
Gem is usually built according to the ".gemspec" file, which is a YAML file containing information about the Gem. Ruby code can also create gems directly, in which case Rake is usually used.
gem command
The gem command is used to build, upload, download and install Gem packages.
gem usage
RubyGems are very similar in functionality to apt-get, portage, yum, and npm.
Install:
gem install mygem
uninstall:
gem uninstall mygem
List installed gems:
gem list --local
List available gems, for example:
gem list --remote
Create RDoc documents for all gems:
gem rdoc --all
Download a gem but don't install it:
gem fetch mygem
Search from available gems, for example:
gem search STRING --remote
The gem package's build
gem command is also used to build and maintain .gemspec and .gem files.
Build .gem using .gemspec file:
gem build mygem.gemspec
Modifying Domestic Sources
Due to domestic network reasons (you know it), the resource files stored by rubygems.org on Amazon S3 failed to connect intermittently.
Therefore, you will encounter gem install rack or bundle install without response for half a day. Specifically, you can use gem install rails -V to view the execution process.
Therefore we can modify it to the domestic download source: https://gems.ruby-china.com
First, check the current source:
$ gem sources -l
*** CURRENT SOURCES ***
https://rubygems.org/
Next, remove https://rubygems.org/ and add the domestic download source https://gems.ruby-china.com/.
$ gem sources --remove https://rubygems.org/
$ gem sources -a https://gems.ruby-china.com/
$ gem sources -l
*** CURRENT SOURCES ***
https://gems.ruby-china.com/
# 请确保只有 gems.ruby-china.com
$ gem install rails
If you use a Gemfile and a Bundle (for example: a Rails project)
you can use the bundle's gem source code mirror command.
$ bundle config mirror.https://rubygems.org https://gems.ruby-china.com/
This way you don't have to change the source of your Gemfile.
source 'https://rubygems.org/'
gem 'rails', '4.1.0'
...