Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

preserving nulls in int vectors #30

Open
thoughtafter opened this issue Sep 11, 2018 · 1 comment
Open

preserving nulls in int vectors #30

thoughtafter opened this issue Sep 11, 2018 · 1 comment

Comments

@thoughtafter
Copy link

I noticed this inconsistent behavior:

[1] pry(main)> require 'rserve'
=> true
[2] pry(main)> Rserve::REXP::Integer.new([nil, nil]).to_ruby
=> [0, 0]
[3] pry(main)> Rserve::REXP::Double.new([nil, nil]).to_ruby
=> [nil, nil]

It seems int vectors lose nulls and double vectors keep them. This is due to:
https://github.com/clbustos/Rserve-Ruby-client/blob/master/lib/rserve/rexp/integer.rb#L14

 data.map {|v| v.to_i}

Since nil.to_i == 0 this will wipe out nulls.

@thoughtafter
Copy link
Author

So I thought I have fixed this in code that I'm using and found it's trickier than I had first guessed.

require 'rserve'
con=Rserve::Connection.new
con.assign("v", Rserve::REXP::Integer.new([1, 2, nil, nil])).to_ruby
con.eval("v").to_ruby

The nil's are replaced with zeros. However, if
data.map {|v| v.to_i} is changed to say:
data.map {|v| v.try(:to_int)} the nils are preserved in the Rserve::REXP::Integer.new but the assignment will fail due to calling Protocol.set_int with a non-integer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant