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

249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-06-19 18:28
(Received via mailing list)
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 Rakshit
================
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
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2014-06-19 20:32
(Received via mailing list)
Excerpts from Arup Rakshit'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
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-06-19 21:05
(Received via mailing list)
On Thursday, June 19, 2014 08:38:38 PM Stefano Crocco 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 Rakshit
================
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
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2014-06-19 21:17
(Received via mailing list)
Excerpts from Arup Rakshit'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
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-06-19 21:27
(Received via mailing list)
On Thursday, June 19, 2014 09:23:54 PM Stefano Crocco 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. :)

--
================
Regards,
Arup Rakshit
================
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
3df767279ce7d81db0a5bb30f5136863?d=identicon&s=25 Matthew Kerwin (mattyk)
on 2014-06-19 22:22
(Received via mailing list)
On 20/06/2014, Arup Rakshit <aruprakshit@rocketmail.com> 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.
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.