https://github.com/kontena/yaml-safe_load_stream
Adds YAML.safe_load_stream to Ruby YAML
https://github.com/kontena/yaml-safe_load_stream
ruby yaml
Last synced: about 1 year ago
JSON representation
Adds YAML.safe_load_stream to Ruby YAML
- Host: GitHub
- URL: https://github.com/kontena/yaml-safe_load_stream
- Owner: kontena
- License: apache-2.0
- Created: 2018-12-20T10:45:04.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2021-07-02T18:47:07.000Z (almost 5 years ago)
- Last Synced: 2025-04-12T17:12:32.320Z (about 1 year ago)
- Topics: ruby, yaml
- Language: Ruby
- Size: 23.4 KB
- Stars: 0
- Watchers: 3
- Forks: 8
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# YAMLSafeLoadStream
[](https://travis-ci.org/kontena/yaml-safe_load_stream)
[](https://badge.fury.io/rb/yaml-safe_load_stream)
[](http://www.rubydoc.info/github/kontena/yaml-safe_load_stream/master)
The Ruby standard library YAML/Psych module defines `YAML.safe_load` for safely loading YAML documents
There's also `YAML.load_stream` for loading multi-document streams.
The problem is, there's no `YAML.safe_load_stream` that would safely load a stream with multiple documents.
This gem adds a very bare bones implementation of that missing feature.
## Usage
There is just one method: `safe_load_stream(yaml_content, optional_filename_to_be_used_in_exception_messages)`
When used with a block, it will yield each document separately:
```ruby
YAMLSafeLoadStream.safe_load_stream("---\nhello: world\n\n---\nhello: universe\n") do |document|
puts document.inspect
end
# outputs:
# { 'hello' => 'world' }
# { 'hello' => 'universe' }
```
When used without a block, it will return an Array containing documents in the stream:
```ruby
docs = YAMLSafeLoadStream.safe_load_stream("---\nhello: world\n\n---\nhello: universe\n")
puts docs.inspect
# outputs:
# [{ 'hello' => 'world' }, { 'hello' => 'universe' }]
```
### Directly
```ruby
require 'yaml/safe_load_stream'
YAMLSafeLoadStream.safe_load_stream("---\nhello: world\n\n---\nhello: universe\n")
```
### As a refinement
```ruby
require 'yaml/safe_load_stream'
using YAMLSafeLoadStream
YAML.safe_load_stream("---\nhello: world\n\n---\nhello: universe\n")
```
### Requiring the `core-ext`
```ruby
require 'yaml/safe_load_stream/core-ext'
YAML.safe_load_stream("---\nhello: world\n\n---\nhello: universe\n")
```
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'yaml-safe_load_stream'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install yaml-safe_load_stream
## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/kontena/yaml-safe_load_stream