From: Robert Klemme Date: 2013-06-17T20:04:57+09:00 Subject: Re: Issue in reading data from yaml file --001a11c30b18111a5d04df5791d7 Content-Type: text/plain; charset=ISO-8859-1 On Sun, Jun 16, 2013 at 9:10 PM, Hassan Schroeder < hassan.schroeder@gmail.com> wrote: > On Sun, Jun 16, 2013 at 11:43 AM, Rochit Sen 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. > 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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --001a11c30b18111a5d04df5791d7 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Sun, Jun 16, 2013 at 9:10 PM, Hassan Schroeder <hassan= .schroeder@gmail.com> wrote:
On Sun, Jun 16, 2013 at 11:43 AM, Rochit= Sen <lists@ruby-forum.com&g= t; wrote:

> require 'yaml'
> data =3D YAML.load('config.yml')
> @path =3D 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.

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
<= div>- 0
- 1
$ ruby -r yaml -e 'p File.open("x&= quot;, encoding: "UTF-8") {|io| YAML.load(io)}'
[8, 5, 0, 0, 1]

=A0
=A0 data =3D YAML.load(File.read('config.yml'))

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

Kind regards

robert


--
remember.guy do |as, often| as.you_can - without end
= http://blog.rubybestpractice= s.com/
--001a11c30b18111a5d04df5791d7--