Robert said:
Nick said:
i am making a pdf file, retrieving an product for the db and print
the price in the pdf
@product = Product.find(@params["id"])
pdf.SetFont('Arial','',10)
pdf.Cell(3,h, "?",0,0)
pdf.Cell(100,h, " " + @product.price , 0,1)
pdf.Cell(100,h, @product.price.to_s , 0,1)
pdf.Cell(100,h, " %4.1f" % @product.price , 0,1)
pdf.Cell(100,h, sprintf(" %4.1f", @product.price) , 0,1)
Add to that:
pdf.Cell(100,h, "#{@product.price}", 0,1)
The technical explanation is that Ruby does not automatically cast
between basic Classes. For that matter, since types are never declared,
Ruby has no way of knowing which Class you want. So you tell it which
Class you want, by calling #to_s on the Object, which asks the (Float in
this case), "I want a String representation of you."
Some builtin methods (such as #puts) will automatically call #to_s on
the object to get its String representation, though. Every Object has a
#to_s method, which, by default, returns something like
"#<Object:0xfed89350". Some Objects, such as String and Float, override
this method with their own version. (String#to_s returns self.)
If you got through all that, then you'll be fine.
If you didn't, I'll
be happy to expound.
Devin