From: Robert Klemme on 21 Jul 2010 06:55 2010/7/21 Xavier Noria <fxn(a)hashref.com>: > On Wed, Jul 21, 2010 at 12:12 PM, Tom Ha <tom999(a)gmx.net> wrote: > >> what's the most simple solution in Ruby to check if value x is a >> multiple of y? > > That's typically done with the modulus operator, Active Support > defines it this way > > class Integer > # Check whether the integer is evenly divisible by the argument. > def multiple_of?(number) > number != 0 ? self % number == 0 : zero? > end > end > > It is special-cased because you can't do modulus 0. I see a benchmark lurking: that version vs. this one. class Integer def multiple_off? number zero? || self % number == 0 end end :-) Don't have the time right now... Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
From: Joseph E. Savard on 21 Jul 2010 10:26 x = 9 y = 3 puts "yep" if (x%y).zero? x = 9 y = 2 puts "nope" if !(x%y).zero? > From: Tom Ha <tom999(a)gmx.net> > Reply-To: <ruby-talk(a)ruby-lang.org> > Newsgroups: comp.lang.ruby > Date: Wed, 21 Jul 2010 19:12:30 +0900 > To: ruby-talk ML <ruby-talk(a)ruby-lang.org> > Subject: How to check if x is a multiple of y > > Hi there, > > what's the most simple solution in Ruby to check if value x is a > multiple of y? > > "if x/y.integer?" > > ...is obviously not the solution. > > Thanks a lot! > Tom > -- > Posted via http://www.ruby-forum.com/. >
From: Robert Klemme on 21 Jul 2010 13:43 On 21.07.2010 13:20, Xavier Noria wrote: > On Wednesday, July 21, 2010, Robert Klemme<shortcutter(a)googlemail.com> wrote: >> 2010/7/21 Xavier Noria<fxn(a)hashref.com>: >>> On Wed, Jul 21, 2010 at 12:12 PM, Tom Ha<tom999(a)gmx.net> wrote: >>> >>>> what's the most simple solution in Ruby to check if value x is a >>>> multiple of y? >>> >>> That's typically done with the modulus operator, Active Support >>> defines it this way >>> >>> class Integer >>> # Check whether the integer is evenly divisible by the argument. >>> def multiple_of?(number) >>> number != 0 ? self % number == 0 : zero? >>> end >>> end >>> >>> It is special-cased because you can't do modulus 0. >> >> I see a benchmark lurking: that version vs. this one. >> >> class Integer >> def multiple_off? number >> zero? || self % number == 0 >> end >> end >> >> :-) > > But that one is not well-defined for 1.multiple_of?(0) :-) There you see why posting in a hurry is a bad idea. Thanks for catching that. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/
From: Colin Bartlett on 21 Jul 2010 14:49 [Note: parts of this message were removed to make it a legal post.] On Wed, Jul 21, 2010 at 6:45 PM, Robert Klemme <shortcutter(a)googlemail.com>wrote: > On 21.07.2010 13:20, Xavier Noria wrote: > >> On Wednesday, July 21, 2010, Robert Klemme<shortcutter(a)googlemail.com> >> wrote: >> >>> ... >> >> I see a benchmark lurking: that version vs. this one. >>> class Integer >>> def multiple_off? number >>> zero? || self % number == 0 >>> end >>> end >>> >>> :-) >> >> >> But that one is not well-defined for 1.multiple_of?(0) :-) >> > > There you see why posting in a hurry is a bad idea. Thanks for catching > that. For what they are worth (which is very little as I distrust benchmarks which haven't been run sufficiently many times to generate reliable standard deviations, not to mention my Windows Vista installation doing even more apparently random and purposeless disk i/o than usual) these benchmarks suggest that (ignoring the minor bug!) multiple_off? was usually slightly slower than multiple_of? on my system about two hours ago! ruby 1.9.1p243 (2009-07-16 revision 24175) [i386-mingw32] each benchmark is a 1_000_0000.times run; user system total real num= 72 denom= 13 multiple_of? 0.921000 0.000000 0.921000 ( 1.012000) multiple_off? 0.967000 0.000000 0.967000 ( 1.177000) multiple_of? 0.827000 0.015000 0.842000 ( 0.992000) multiple_off? 0.983000 0.000000 0.983000 ( 1.110000) num= 72 denom= 12 multiple_of? 0.874000 0.000000 0.874000 ( 1.072000) multiple_off? 0.967000 0.000000 0.967000 ( 1.182000) multiple_of? 0.936000 0.000000 0.936000 ( 1.090000) multiple_off? 1.030000 0.000000 1.030000 ( 1.156000) num= 0 denom= 0 multiple_of? 0.874000 0.000000 0.874000 ( 1.108000) multiple_off? 0.780000 0.000000 0.780000 ( 1.004000) multiple_of? 0.827000 0.000000 0.827000 ( 1.122000) multiple_off? 0.889000 0.000000 0.889000 ( 1.043000) num= 72 denom= 0 multiple_of? 0.905000 0.000000 0.905000 ( 1.114000) multiple_of? 0.936000 0.000000 0.936000 ( 1.088000) multiple_off? #<ZeroDivisionError: divided by 0>
First
|
Prev
|
Pages: 1 2 Prev: File class documentation Next: how to make "gem install rmagick" work? |