[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Extending Float

Jp Hastings-spital

5/21/2009 3:12:00 PM

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
--
Posted via http://www.ruby-....

5 Answers

Andrew Timberlake

5/21/2009 3:29:00 PM

0

On Thu, May 21, 2009 at 5:11 PM, Jp Hastings-spital
<jphastings@gmail.com> 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
> =A0def to_s(decimalplaces =3D 0)
> =A0 =A0(((self * 10**(decimalplaces+2)).round)/10**decimalplaces).to_s+"%=
"
> =A0end
> end
>
> puts Percentage.new(0.5)
> --
> I just get the following error:
> =A0NoMethodError: undefined method =91new=92 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 =3D 0)
(((self * 10**(decimalplaces+2)).round)/10**decimalplaces).to_s+"%"
end
end

percentage =3D Percentage.new(0.5)
percentage.to_s #=3D> "50%"

Andrew Timberlake
http://ramblingso...
http://www.linkedin.com/in/andrew...

"I have never let my schooling interfere with my education" - Mark Twain

Jp Hastings-spital

5/21/2009 4:04:00 PM

0

Thanks so much - and for being so quick!
--
Posted via http://www.ruby-....

Brian Candler

5/21/2009 4:04:00 PM

0

Jp Hastings-spital wrote:
> puts Percentage.new(0.5)
> --
> I just get the following error:
> NoMethodError: undefined method â??newâ?? for Percentage:Class

That's because you can't do Float.new either.
--
Posted via http://www.ruby-....

Jp Hastings-spital

5/22/2009 5:31:00 PM

0

Brian Candler wrote:
> That's because you can't do Float.new either.

So, out of interest, how does a Float get initialized (what function is
called)? Or is that some fancy inbuilt something-or-other?
Logically I'd assume I'd be able to do
my_pc = Percentage = 0.5
or something along those lines. What do you reckon?
--
Posted via http://www.ruby-....

Robert Dober

5/22/2009 7:26:00 PM

0

On Fri, May 22, 2009 at 7:30 PM, Jp Hastings-spital
<jphastings@gmail.com> wrote:
> Brian Candler wrote:
>> That's because you can't do Float.new either.
>
> So, out of interest, how does a Float get initialized (what function is
> called)? Or is that some fancy inbuilt something-or-other?
> Logically I'd assume I'd be able to do
> =A0my_pc =3D Percentage =3D 0.5
> or something along those lines. What do you reckon?
This is what happenes in numeric.c
rb_cFloat =3D rb_define_class("Float", rb_cNumeric);

rb_undef_alloc_func(rb_cFloat);
rb_undef_method(CLASS_OF(rb_cFloat), "new");

Probably for some good reasons, however *you* are king

irb(main):001:0> class Float
irb(main):002:1> def self.new x
irb(main):003:2> Float( x )
irb(main):004:2> end
irb(main):005:1> end
=3D> nil
irb(main):006:0> class W < Float
irb(main):007:1> end
=3D> nil
irb(main):008:0> x=3DW::new 42
=3D> 42.0

How floats get initialized? I dunno, but it is my guess that the
parser generates some code very similar to what is in Kernel#Float.
But for some reason my grep skills elude me to find the code of
Kernel#Float.

> --
> Posted via http://www.ruby-....
>
>



--=20
Toutes les grandes personnes ont d=92abord =E9t=E9 des enfants, mais peu
d=92entre elles s=92en souviennent.

All adults have been children first, but not many remember.

[Antoine de Saint-Exup=E9ry]