Issue in reading data from yaml file

Hi All,

I have a very small yaml file as below:

“logfile”: C:\examples\code\launchy\devtrack\Dev_Weekly\log.txt

In my ruby program i want to read the location of a file as specified in
the yaml file above. I am writing the following code:

require ‘yaml’
data = YAML.load(‘config.yml’)
@path = data[“logfile”]
puts @path

But this is not showing anything in console, just blank. What would be
the way to read from the values in a yaml file?
Also a yaml file is saved with extension .yml or .yaml. I have seen both
used in some examples.

Help much appreciated.

Hassan S. wrote in post #1112595:

data = YAML.load(File.read(‘config.yml’))

… or use YAML.load_file

On Sun, Jun 16, 2013 at 9:10 PM, Hassan S. <
[email protected]> wrote:

If you (ahem) read the docs, YAML.load works on strings, not files,
so e.g.

Does it?

$ ruby -r yaml -e ‘File.open(“x”, “w”, encoding: “UTF-8”) {|io|
YAML.dump(Array.new(5){rand(10)}, io)}’
$ cat x

  • 8
  • 5
  • 0
  • 0
  • 1
    $ ruby -r yaml -e ‘p File.open(“x”, encoding: “UTF-8”) {|io|
    YAML.load(io)}’
    [8, 5, 0, 0, 1]

data = YAML.load(File.read(‘config.yml’))

I’d rather use the form with the IO object because that avoids the
overhead
of reading the whole file into memory at once.

Kind regards

robert

Am 16.06.2013 20:43, schrieb Rochit S.:

puts @path

But this is not showing anything in console, just blank.

Two tips for analyzing what goes wrong:

  1. Try it in irb:

    2.0.0-p195 :006 > data = YAML.load(‘config.yml’)
    => “config.yml”

    and you will see that YAML.load parses the argument string,
    not the file content

  2. alternatively: insert puts (or better p) statements in your code,
    to see what is going on:

    data = YAML.load(‘config.yml’)
    p data

One solution would be to use YAML.load_file, like already suggested.

On Sun, Jun 16, 2013 at 11:43 AM, Rochit S. [email protected]
wrote:

require ‘yaml’
data = YAML.load(‘config.yml’)
@path = data[“logfile”]
puts @path

But this is not showing anything in console, just blank. What would be
the way to read from the values in a yaml file?

If you (ahem) read the docs, YAML.load works on strings, not files,
so e.g.

data = YAML.load(File.read(‘config.yml’))

HTH,

On Mon, Jun 17, 2013 at 4:22 PM, Hassan S. <
[email protected]> wrote:

require ‘yaml’ # STEP ONE, REQUIRE YAML!

Parse a YAML string

YAML.load("— foo") #=> “foo”

Seems straightforward enough :slight_smile:

Well, it’s just that: an example. To be fair, the documentation is in
dire
need of improvement.

But as you can indeed see from my example (or trying it out yourself) it
works not only on YAML strings but also IO objects.

I’d rather use the form with the IO object because that avoids the
overhead
of reading the whole file into memory at once.

The phrase “premature optimization” comes to mind, given the
example under discussion, but whatever floats your boat :slight_smile:

Given that the code isn’t really that much more complicated I’d barely
speak of an optimization - it’s more like a habit for me.

Cheers

robert

Hi All,
Thanks for the help. YAML.load_file() worked.
Cheers!

On Mon, Jun 17, 2013 at 4:04 AM, Robert K.
[email protected] wrote:

If you (ahem) read the docs, YAML.load works on strings, not files,

Does it?

Via the Module: YAML (Ruby 1.9.3)
“Usage” example:

require ‘yaml’ # STEP ONE, REQUIRE YAML!

Parse a YAML string

YAML.load(“— foo”) #=> “foo”

Seems straightforward enough :slight_smile:

I’d rather use the form with the IO object because that avoids the overhead
of reading the whole file into memory at once.

The phrase “premature optimization” comes to mind, given the
example under discussion, but whatever floats your boat :slight_smile: