Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I parse YAML with Ruby?

Tags:

ruby

yaml

This is my Ruby code:

require 'yaml'
yaml = YAML.parse(
  '''
  foo: "hello, world"
  '''
)
puts yaml['foo']

I'm getting:

NoMethodError: undefined method `[]' for #<Psych::Nodes::Document:0x007f92a4404d98>

It's Ruby 2.1.3

like image 635
Barbara Krein Avatar asked Dec 19 '14 20:12

Barbara Krein


1 Answers

You should use YAML.load instead of YAML.parse according to documentation to parse the YAML.

require 'yaml'
yaml = YAML.load(
  '''
  foo: "hello, world"
  '''
)
puts yaml['foo']

# => hello, world
like image 196
Rustam A. Gasanov Avatar answered Sep 30 '22 05:09

Rustam A. Gasanov