Reputation: 83
I created method "to_s" :
def to_s
"#{self.name}:#{self.price}:#{self.weight}"
end
When I add products to the cart by "cart.add_item"
@items = []
@items << AntiqueItem.new({:price => 111, :weight => 100, :name => 'car'})
@items << RealItem.new({:price => 111, :weight => 100, :name => 'kettle'})
@items << RealItem.new({:price => 200, :weight => 100, :name => 'dishwasher'})
cart = Cart.new('vitalik')
cart.add_item RealItem.new({:price => 111, :weight => 100, :name => 'car'})
cart.add_item RealItem.new({:price => 121, :weight => 100, :name => 'car'})
cart.add_item RealItem.new({:price => 151, :weight => 100, :name => 'kettle'})
p cart
In the console information is not displayed string
E:\work\storeapp3\app\storeapp\shop>ruby init.rb
#<Cart:0x2685888 @items=[#<RealItem:0x2685828 @weight=100, @real_price=111, @name="car">, #<RealItem:0x2685738 @weight=100, @real_price=121, @name="car">, #<RealItem:0x2685648 @wei
ght=100, @real_price=151, @name="kettle">], @owner="vitalik">
Maybe I made a mistake somewhere?
Upvotes: 0
Views: 60
Reputation: 51151
It's because p
displays the result of calling inspect
method on object. You can use puts
instead.
Upvotes: 5