Class: Cooklang::Cookware

Inherits:
Object
  • Object
show all
Defined in:
lib/cooklang/cookware.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, quantity: nil) ⇒ Cookware

Returns a new instance of Cookware.



7
8
9
10
# File 'lib/cooklang/cookware.rb', line 7

def initialize(name:, quantity: nil)
  @name = name.to_s.freeze
  @quantity = quantity
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/cooklang/cookware.rb', line 5

def name
  @name
end

#quantityObject (readonly)

Returns the value of attribute quantity.



5
6
7
# File 'lib/cooklang/cookware.rb', line 5

def quantity
  @quantity
end

Instance Method Details

#==(other) ⇒ Object



25
26
27
28
29
# File 'lib/cooklang/cookware.rb', line 25

def ==(other)
  return false unless other.is_a?(Cookware)

  name == other.name && quantity == other.quantity
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/cooklang/cookware.rb', line 31

def eql?(other)
  self == other
end

#has_quantity?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/cooklang/cookware.rb', line 39

def has_quantity?
  !@quantity.nil?
end

#hashObject



35
36
37
# File 'lib/cooklang/cookware.rb', line 35

def hash
  [name, quantity].hash
end

#to_hObject



18
19
20
21
22
23
# File 'lib/cooklang/cookware.rb', line 18

def to_h
  {
    name: @name,
    quantity: @quantity
  }.compact
end

#to_sObject



12
13
14
15
16
# File 'lib/cooklang/cookware.rb', line 12

def to_s
  result = @name
  result += " (#{@quantity})" if @quantity
  result
end