Skip to content

CI build fail fix #1

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions lib/numruby/nmatrix.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,46 @@ def to_a
return self.elements
end

def _dump data
[
self.dim,
self.dtype,
self.stype,
self.shape,
self.elements,
].join(":")
end

def self._load serial
data = serial.split(":")
dim = data[0].to_i
dtype = data[1].to_sym
stype = data[2].to_sym
shape = data[3..(3+dim-1)]
elements = data[(3+dim)..data.length-1]
parsed_elements = []

if dtype == :nm_bool
elements.each do |e|
parsed_elements.push(e == "true" ? true : false)
end
elsif dtype == :nm_float64
elements.each do |e|
parsed_elements.push(e.to_f)
end
else
# Convert to Integer
elements.each do |e|
parsed_elements.push(e.to_i)
end
end
parsed_shape = []
shape.each do |e|
parsed_shape.push(e.to_i)
end
self.new(parsed_shape, parsed_elements, dtype)
end

def inspect #:nodoc:
original_inspect = super()
original_inspect = original_inspect[0...original_inspect.size-1]
Expand Down
12 changes: 12 additions & 0 deletions test/nmatrix_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,16 @@ def test_slicing
assert_equal @m_int[0, 0..1, 0..1], @s_int
end

def test_serializing
serialized_data_float = Marshal.dump(@m)
deserialized_data_float = Marshal.load(serialized_data_float)
serialized_data_int = Marshal.dump(@m_int)
deserialized_data_int = Marshal.load(serialized_data_int)
serialized_data_bool = Marshal.dump(@b)
deserialized_data_bool = Marshal.load(serialized_data_bool)
assert_equal @m, deserialized_data_float
assert_equal @m_int, deserialized_data_int
assert_equal @b, deserialized_data_bool
end

end