Why does YAML::load converting nil object to 'nil' while loading

Hi,

I am not understanding why YAML::load converting nil object to
string
object always like ‘nil’ ?

2.0.0-p451 :001 > require ‘yaml’
=> true
2.0.0-p451 :002 > YAML.load “[nil, nil, nil]”
=> [“nil”, “nil”, “nil”]
2.0.0-p451 :008 > YAML.load “[true, false]”
=> [true, false]
2.0.0-p451 :009 > YAML.load “[true, false, nil]”
=> [true, false, “nil”]
2.0.0-p451 :010 >

From the above code I can see only nil object is getting converted to
“nil” . Is this behavior implemented intentionally ?

Regards,
Arup R.

Debugging is twice as hard as writing the code in the first place.
Therefore,
if you write the code as cleverly as possible, you are, by definition,
not
smart enough to debug it.

–Brian Kernighan

Excerpts from Arup R.'s message of 2014-06-19 17:27:20 +0200:

=> [true, false]
2.0.0-p451 :009 > YAML.load “[true, false, nil]”
=> [true, false, “nil”]
2.0.0-p451 :010 >

From the above code I can see only nil object is getting converted to
“nil” . Is this behavior implemented intentionally ?

Because in YAML, the value nil is not represented by the literal nil but
with
the character ~. The boolean values true and false are instead
represented by
the literals true and false respectively. To obtain what you want you
need to
use:

YAML.load “[true, false, ~]”

I hope this helps

Stefano

On Thursday, June 19, 2014 08:38:38 PM Stefano C. wrote:

Because in YAML, the value nil is not represented by the literal nil but
with the character ~. The boolean values true and false are instead
represented by the literals true and false respectively.

Great information. I was also reading one online YAML doc, but I didn’t
find
there what you said.

To obtain what you
want you need to use:

YAML.load “[true, false, ~]”

I hope this helps

Problem is data is coming from outside, I just want it to convert to an
Array,
so that I can work on it further. But it is changing original data. Is
there
any other trick to handle this ?

Regards,
Arup R.

Debugging is twice as hard as writing the code in the first place.
Therefore,
if you write the code as cleverly as possible, you are, by definition,
not
smart enough to debug it.

–Brian Kernighan

On Thursday, June 19, 2014 09:23:54 PM Stefano C. wrote:

I’m not sure I understand you correctly.

You got me correctly.

What you can do to have the string
“nil” replaced by nil in the arrays, is to look into them after loading from
YAML and replace the former with the latter:

a = YAML.load “[true, false, nil]”
a.map!{|x| x == “nil” ? nil : x}

That option I also thought. But in that case, if any real data come as
“nil”,
I will by mistake convert it to nil, which is a lose of data. :slight_smile:

Regards,
Arup R.

Debugging is twice as hard as writing the code in the first place.
Therefore,
if you write the code as cleverly as possible, you are, by definition,
not
smart enough to debug it.

–Brian Kernighan

On 20/06/2014, Arup R. [email protected] wrote:

Problem is data is coming from outside, I just want it to convert to an
Array,
so that I can work on it further. But it is changing original data. Is there

any other trick to handle this ?

How is it coming from outside? If it’s not coming as yaml (as appears
to be the case) you have to either coerce it before feeding it to
YAML::load, or use a different parse/load library.

If it’s meant to be yaml, but includes invalid nils, you could throw
the error back at the data producers. Of course, you’d have to assume
(or know through out-of-band discussion) that the nils are invalid to
start with.

Excerpts from Arup R.'s message of 2014-06-19 20:05:03 +0200:

To obtain what you
want you need to use:

YAML.load “[true, false, ~]”

I hope this helps

Problem is data is coming from outside, I just want it to convert to an Array,
so that I can work on it further. But it is changing original data. Is there
any other trick to handle this ?

I’m not sure I understand you correctly. What you can do to have the
string
“nil” replaced by nil in the arrays, is to look into them after loading
from
YAML and replace the former with the latter:

a = YAML.load “[true, false, nil]”
a.map!{|x| x == “nil” ? nil : x}

Stefano