Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Problems with a code I wrote

I have written this code for a simple library managment course. Here I want to do 2 things which I have not been able to do:

  1. I want the program to throw an ArgumentError if the price < 0 or if isbn is empty. I have done that but isn't there a way to do it using raise_error which I guess is the correct way.
  2. I want the price to be displayed with precision of 2. I can't do that also though I have tried to with sprintf Any help would be highly appreciated


class BookInStock
attr_reader :isbn, :price

def initialize(isbn = 0, price = "")
@isbn = isbn
@price = price
sprintf("%.2f" % @price)

def isbn=(isbn)

def isbn()

def price=(price)
number_with_precision(price, :precision => 2)
#sprintf("%.2f" % @price.to_f)

def price()

def price_as_string
raise ArgumentError unless price > 0 
raise ArgumentError if isbn == "" 
#sprintf("%.2f" % @price)
#val = 9.756
#sprintf("%.2f" % val)
#print "#{val}"
puts "$#{price} #{isbn}"
return price

book_in = BookInStock.new('', 25.00)