From 254daba12d6a34eff59422ff235aa9cf197b33a0 Mon Sep 17 00:00:00 2001 From: Florian Ebeling Date: Sat, 21 Mar 2015 19:25:43 +0100 Subject: [PATCH] Fix bug when comparing node with nil --- CHANGELOG.md | 4 ++++ lib/libsvm/node.rb | 8 +++++--- spec/node_spec.rb | 4 ++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aa1c73a..5c01d42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/). +## [Unreleased][unreleased] +### Fixed +- Fix a bug in `Libsvm::Node#==` when comparing with `nil`. + ## [1.3.1] - 2015-03-21 ### Fixed - Spelling and punctuation in documentation comments diff --git a/lib/libsvm/node.rb b/lib/libsvm/node.rb index c02fe62..f31f7b4 100644 --- a/lib/libsvm/node.rb +++ b/lib/libsvm/node.rb @@ -90,13 +90,15 @@ def initialize(index=0, value=0.0) self.value = value end - # Compare features for equality. + # Compare feature node for equality. # # Nodes with equal index and value are equal. # # @return [Boolean] - def ==(other) - index == other.index && value == other.value + def == (other) + other.class == self.class && + index == other.index && + value == other.value end def inspect diff --git a/spec/node_spec.rb b/spec/node_spec.rb index 1e70e5f..5b3cb7f 100644 --- a/spec/node_spec.rb +++ b/spec/node_spec.rb @@ -67,6 +67,10 @@ expect(ary.map(&:index)).to eq([3, 5, 6, 10]) end + it "compares with nil" do + expect(Node.new(1,2)).to_not eq(nil) + end + it "implements value equality" do expect(Node[1, 0.1]).to eq(Node[1, 0.1]) end