From: Daniel Martin on 2 May 2007 16:18 Just one minor nit: Christian Neukirchen writes: > def luhn?(n) > f = 2 > (n.delete("^0-9").reverse.split(//).map{|d|d.to_i}. > inject(0) { |a,e| f=3-f; a + (e*f > 9 ? e*f-9 : e*f) } % 10).zero? > end You do know that (e*f > 9 ? e*f-9 : e*f) is equivalent to e*f%9, right? So that makes this method: def luhn?(n) f = 2 (n.delete("^0-9").reverse.split(//).map{|d|d.to_i}. inject(0) { |a,e| f=3-f; a + e*f%9 } %10).zero? end (I still like my short version better, but too each his own) -- s=%q( Daniel Martin -- martin(a)snowplow.org puts "s=%q(#{s})",s.to_a[1] ) puts "s=%q(#{s})",s.to_a[1] From: James Edward Gray II on 2 May 2007 16:27 On May 2, 2007, at 3:18 PM, Daniel Martin wrote: > Just one minor nit: > > Christian Neukirchen writes: > >> def luhn?(n) >> f = 2 >> (n.delete("^0-9").reverse.split(//).map{|d|d.to_i}. >> inject(0) { |a,e| f=3-f; a + (e*f > 9 ? e*f-9 : e*f) } % >> 10).zero? >> end > > You do know that (e*f > 9 ? e*f-9 : e*f) is equivalent to e*f%9, > right? >> (0..9).map { |n| n * 2 % 9 } => [0, 2, 4, 6, 8, 1, 3, 5, 7, 0]>> (0..9).map { |n| n * 2 > 9 ? n * 2 - 9 : n * 2 } => [0, 2, 4, 6, 8, 1, 3, 5, 7, 9] James Edward Gray II From: Paul Novak on 2 May 2007 21:47 Thank you James for another fun quiz. My solution is a more verbose version of the metaprogramming-based routing used in why's excellent camping. But instead of regexen matching urls to controller classes, here they are matching card numbers to the card classes. What I like about this approach is that you can add more card patterns just by adding more classes with the appropriate regexen, and you can override validation or other functionality as needed. Regards, Paul. # cardvalidator.rb # require 'metaid' module Card @cards=[] def Card.Base *u c = @cards Class.new { meta_def(:patterns){u} meta_def(:validity){|x|Card.validity(x)} meta_def(:inherited){|x|c< Ruby Quiz will now take a one week break. Work has been rough this week and I> need some down time. I'll be back next week, rested, and with new quizzes... Means that: no quiz tomorrow ?!? -- greets ( ) ( /\ .-"""-. /\ //\\/ ,,, \//\\ |/\| ,;;;;;, |/\| //\\\;-"""-;///\\ // \/ . \/ \\ (| ,-_| \ | / |_-, |) //`__\.-.-./__`\\ // /.-(() ())-.\ \\ (\ |) '---' (| /) ` (| |) ` jgs \) (/ one must still have chaos in oneself to be able to give birth to a dancing star First  |  Prev  |  Next  |  Last