From: Andrew Timberlake Date: 2009-05-22T00:29:06+09:00 Subject: Re: Extending Float On Thu, May 21, 2009 at 5:11 PM, Jp Hastings-spital wrote: > Maybe I haven't wrapped my head around OOP in the way I should have, can > anyone explain to me why I can't do this?: > -- > class Percentage < Float >  def to_s(decimalplaces = 0) >    (((self * 10**(decimalplaces+2)).round)/10**decimalplaces).to_s+"%" >  end > end > > puts Percentage.new(0.5) > -- > I just get the following error: >  NoMethodError: undefined method ‘new’ for Percentage:Class > > I hope its obvious what I'm trying to do, let me know if there's a way > to achieve this that I'm missing! > Thanks in advance Use a delegate class require 'delegate' class Percentage < DelegateClass(Float) def to_s(decimalplaces = 0) (((self * 10**(decimalplaces+2)).round)/10**decimalplaces).to_s+"%" end end percentage = Percentage.new(0.5) percentage.to_s #=> "50%" Andrew Timberlake http://ramblingsonrails.com http://www.linkedin.com/in/andrewtimberlake "I have never let my schooling interfere with my education" - Mark Twain