7stud2
(7stud --)
16 June 2013 18:43
1
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.
--
Posted via http://www.ruby-forum.com/ .
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 Sun, Jun 16, 2013 at 11:43 AM, Rochit Sen <lists@ruby-forum.com> 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?
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan
stomar
(stomar)
17 June 2013 14:08
3
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.
···
Am 16.06.2013 20:43, schrieb Rochit Sen:
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.
--
<https://github.com/stomar/> ;
7stud2
(7stud --)
17 June 2013 08:25
4
Hassan Schroeder wrote in post #1112595:
data = YAML.load(File.read('config.yml'))
... or use YAML.load_file
···
--
Posted via http://www.ruby-forum.com/\ .
Does it?
$ ruby -r yaml -e 'File.open("x", "w", encoding: "UTF-8") {|io|
YAML.dump(Array.new(5){rand(10)}, io)}'
$ cat x
···
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> 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.
---
- 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/
If you (ahem) read the docs, YAML.load works on strings, not files,
Does it?
Via the http://ruby-doc.org/stdlib-1.9.3/libdoc/yaml/rdoc/YAML.html
"Usage" example:
require 'yaml' # STEP ONE, REQUIRE YAML!
# Parse a YAML string
YAML.load("--- foo") #=> "foo"
Seems straightforward enough
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
···
On Mon, Jun 17, 2013 at 4:04 AM, Robert Klemme <shortcutter@googlemail.com> wrote:
--
Hassan Schroeder ------------------------ hassan.schroeder@gmail.com
twitter: @hassan
>> 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
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
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
···
On Mon, Jun 17, 2013 at 4:22 PM, Hassan Schroeder < hassan.schroeder@gmail.com> wrote:
On Mon, Jun 17, 2013 at 4:04 AM, Robert Klemme > <shortcutter@googlemail.com> wrote:
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
7stud2
(7stud --)
17 June 2013 17:45
8
Hi All,
Thanks for the help. YAML.load_file() worked.
Cheers!
···
--
Posted via http://www.ruby-forum.com/ .