# Extending Float

**URL:** <https://www.ruby-forum.com/t/extending-float/168506>\
**Category:** Ruby\
**Created:** [May 21, 2009, 5:11pm UTC](https://www.ruby-forum.com/t/extending-float/168506 "2009-05-21T17:11:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jphastings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@jphastings](https://www.ruby-forum.com/u/jphastings)\
**Post date:** [May 21, 2009, 5:11pm UTC](https://www.ruby-forum.com/t/extending-float/168506/1 "2009-05-21T17:11:51Z")

</div>

## 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

---

<div class="post-metadata">

**Author:** ![jphastings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@jphastings](https://www.ruby-forum.com/u/jphastings)\
**Post date:** [May 21, 2009, 5:30pm UTC](https://www.ruby-forum.com/t/extending-float/168506/2 "2009-05-21T17:30:18Z")

</div>

On Thu, May 21, 2009 at 5:11 PM, Jp Hastings-spital  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> –  
> 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 T.  
[http://ramblingsonrails.com](http://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

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

---

<div class="post-metadata">

**Author:** ![jphastings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@jphastings](https://www.ruby-forum.com/u/jphastings)\
**Post date:** [May 21, 2009, 6:04pm UTC](https://www.ruby-forum.com/t/extending-float/168506/3 "2009-05-21T18:04:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jphastings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@jphastings](https://www.ruby-forum.com/u/jphastings)\
**Post date:** [May 21, 2009, 6:03pm UTC](https://www.ruby-forum.com/t/extending-float/168506/4 "2009-05-21T18:03:24Z")

</div>

Thanks so much - and for being so quick!

---

<div class="post-metadata">

**Author:** ![jphastings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@jphastings](https://www.ruby-forum.com/u/jphastings)\
**Post date:** [May 22, 2009, 7:30pm UTC](https://www.ruby-forum.com/t/extending-float/168506/5 "2009-05-22T19:30:37Z")

</div>

Brian C. 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?

---

<div class="post-metadata">

**Author:** ![jphastings](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/3ab097/32.png) [@jphastings](https://www.ruby-forum.com/u/jphastings)\
**Post date:** [May 22, 2009, 9:29pm UTC](https://www.ruby-forum.com/t/extending-float/168506/6 "2009-05-22T21:29:08Z")

</div>

On Fri, May 22, 2009 at 7:30 PM, Jp Hastings-spital  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Brian C. 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?  
> This is what happenes in numeric.c  
> rb\_cFloat = 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  
=\> nil  
irb(main):006:0\> class W \< Float  
irb(main):007:1\> end  
=\> nil  
irb(main):008:0\> x=W::new 42  
=\> 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-forum.com/](http://www.ruby-forum.com/).

–  
Toutes les grandes personnes ont d’abord été des enfants, mais peu  
d’entre elles s’en souviennent.

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

[Antoine de Saint-Exupéry]
