Class: Cooklang::Cookware
- Inherits:
-
Object
- Object
- Cooklang::Cookware
- Defined in:
- lib/cooklang/cookware.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#quantity ⇒ Object
readonly
Returns the value of attribute quantity.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #eql?(other) ⇒ Boolean
- #has_quantity? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(name:, quantity: nil) ⇒ Cookware
constructor
A new instance of Cookware.
- #to_h ⇒ Object
- #to_s ⇒ Object
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
5 6 7 |
# File 'lib/cooklang/cookware.rb', line 5 def name @name end |
#quantity ⇒ Object (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
31 32 33 |
# File 'lib/cooklang/cookware.rb', line 31 def eql?(other) self == other end |
#has_quantity? ⇒ Boolean
39 40 41 |
# File 'lib/cooklang/cookware.rb', line 39 def has_quantity? !@quantity.nil? end |
#hash ⇒ Object
35 36 37 |
# File 'lib/cooklang/cookware.rb', line 35 def hash [name, quantity].hash end |
#to_h ⇒ Object
18 19 20 21 22 23 |
# File 'lib/cooklang/cookware.rb', line 18 def to_h { name: @name, quantity: @quantity }.compact end |
#to_s ⇒ Object
12 13 14 15 16 |
# File 'lib/cooklang/cookware.rb', line 12 def to_s result = @name result += " (#{@quantity})" if @quantity result end |