Class: Object

Inherits:
BasicObject
Defined in:
lib/pleasant_path/json/object.rb,
lib/pleasant_path/yaml/object.rb

Instance Method Summary collapse

Instance Method Details

#write_to_json(file, options = {}) ⇒ self

Dumps the Object as JSON, and writes the JSON to the specified file. Returns the Object unmodified.

For information about available options see JSON.generate.

Examples:

{ "key" => "value" }.write_to_json("out.json")  # == { "key" => "value" }
File.read("out.json")                           # == '{"key":"value"}'


17
18
19
20
21
22
23
24
25
# File 'lib/pleasant_path/json/object.rb', line 17

def write_to_json(file, options = {})
  options = {
    quirks_mode: true,
    allow_nan: true,
  }.merge(options)

  file.to_pathname.write_text(JSON.generate(self, options))
  self
end

#write_to_yaml(file, options = {}) ⇒ self

Dumps the Object as YAML, and writes the YAML to the specified file. Returns the Object unmodified.

For information about available options see YAML.dump.

Examples:

{ "key" => "value" }.write_to_yaml("out.yaml")  # == { "key" => "value" }
File.read("out.yaml")                           # == "---\nkey: value\n"


17
18
19
20
# File 'lib/pleasant_path/yaml/object.rb', line 17

def write_to_yaml(file, options = {})
  File.open(file, 'w'){|f| YAML.dump(self, f, options) }
  self
end