[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Overriding Float.round

rpardee@gmail.com

4/27/2006 8:23:00 PM

Hey All,

Looking at the google groups archive for this group, it seems that
there's no built-in way to round a float to a particular number of
decimal points. I thought I'd use that as an occasion to try my hand
at extending a built-in class, and wrote this:

class Float
def round(places)
num = ("1" + "0"*places.to_i).to_f
(self*num).round/num
end
end

But when I say:

p = 22.0/7
puts p.round(3).to_s

Ruby comes back with:

C:/Documents and Settings/pardre1/Desktop/DelMe.rb:4:in `round': wrong
number of arguments (0 for 1) (ArgumentError)
from C:/Documents and Settings/pardre1/Desktop/DelMe.rb:4:in `round'
from C:/Documents and Settings/pardre1/Desktop/DelMe.rb:9

What am I doing wrong?

Thanks!

-Roy

4 Answers

rpardee@gmail.com

4/27/2006 8:43:00 PM

0

Interestingly--it looks like if I call my method "round2", it works.
But I don't understand why I can't override "round".

(And that original .round method is such a disappoointment to me, I
want to completely obliterate it in my program. ;-)

rpardee@gmail.com

4/27/2006 8:58:00 PM

0

LOL--found my problem. Sorry for polluting the list.

I was misreading the error message to indicate that round was
complaining about getting too *many* arguments--but it was complaining
about getting too *few*--and on the call to Float.round() in my new
version of Float.round(). So this works:

class Float
alias orig_round round
def round(places)
num = ("1" + "0"*places.to_i).to_f
(self*num).orig_round/num
end
end

Anne G

9/29/2010 4:57:00 PM

0

29/09/10 10:50, GAAslin:
> "Anne G" <ann.guille@gmail_anti-spam_.com.invalid> a ?crit dans le
> message de news: 4ca36861@ac-versailles.fr...

>> et si d?licat...

>> ?Plus song? que ?a, tu te mets une branche de c?leri dans le cul pour
>> dire que t'es pas un potager.?

> D?licieux titrez-vous!

> C'est pas un peu cochon ?a?

Cochon, d?licieux et d?licat.

unknown

9/29/2010 8:13:00 PM

0

On Wed, 29 Sep 2010 12:50:05 -0400, "GAAslin" <gaaslin@oricom.ca>
wrote:

>
>"Anne G" <ann.guille@gmail_anti-spam_.com.invalid> a ?crit dans le message
>de news: 4ca36861@ac-versailles.fr...
>> et si d?licat...
>>
>> ?Plus song? que ?a, tu te mets une branche de c?leri dans le cul pour dire
>> que t'es pas un potager.?
>>
>> --
>> Anne
>> Si y'a une sakw? qui n'doit nin d'aller, bin ?a n'dira nin.
>>
>>
>
>D?licieux titrez-vous!
>
>C'est pas un peu cochon ?a?
>
Dans ce cas-ci, c'est une citation hors contexte, c'est un peu cru, je
l'admet, mais ?a image bien ce qu'il avait ?crit pr?c?demment dans sa
chronique.