User defined Objects equality always returns false

user2231215
Class Product
  def initialize(name, qty)
    @name = name
    @qty = qty
  end

  def to_s
    "#{@name}, #{@qty}"
  end
end


irb> Product.new("Amazon", 3) == Product.new ("Amazon", 3)
irb> false

Ruby always returns false for these type of user defined objects which is wrong, how to make them true if they are equal and false if they are not equal

Pol0nium

You should implement the comparison operator.

Example :

Class Product
  attr_reader :name, :qty

  def initialize(name, qty)
    @name = name
    @qty = qty
  end

  def to_s
    "#{@name}, #{@qty}"
  end

  def ==(another_product)
    self.name == another_produc.name and self.qty == another_product.qty
    # or self.to_s == another_product.to_s
  end
end

More info : ruby equality and object comparison


Explanation :

In your example, ruby doesn't know how to compare your object. So ruby compares two adresses (where the objects are stored) and says that the two addresses are different.

If you specify in your class the == operator, ruby now knows how to compare your objects.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to create a Set of user-defined objects in Javascript with a user-defined equality function?

From Dev

Calling an Excel user defined function from Python always returns None

From Dev

user.is_authenticated always returns False for inactive users on template

From Dev

Boolean always returns false

From Dev

setProcessDefaultNetwork always returns false

From Dev

isEqualToString always returns False

From Dev

condition always returns false

From Dev

Regex that always returns false

From Dev

StrPos always returns False?

From Dev

TrySetWallpaperImageAsync() always returns false

From Dev

if ($_POST) always returns false

From Dev

hasNextLine() always returns false

From Dev

Always returns false

From Dev

StrPos always returns False?

From Dev

setATRHistBytes() method always returns false

From Dev

Function returns always Boolean False

From Dev

takeScreenshot always returns false for uiautomator

From Dev

python if statement always returns False

From Dev

Scanner HasNextLine always returns false

From Dev

SharedPreferences variable always returns false

From Dev

in_array always returns false

From Dev

python if statement always returns False

From Dev

auth:: attempt() always returns false

From Dev

Somewhy the program always returns 'false'?

From Dev

Comparing characters always returns false

From Dev

Object returns `false` for both `true` and `false` with the equality operator

From Dev

Always returning false | Javascript | objects

From Dev

Excel VBA User Defined Function That Reads and Returns Data from a Database Doesn't Always Work. Why?

From Dev

Excel VBA User Defined Function That Reads and Returns Data from a Database Doesn't Always Work. Why?

Related Related

HotTag

Archive