<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Credit card type and luhn check in ruby</title>
	<atom:link href="http://www.missiondata.com/blog/ruby/25/credit-card-type-and-luhn-check-in-ruby/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.missiondata.com/blog/web-development/25/credit-card-type-and-luhn-check-in-ruby/</link>
	<description>Louisville-based Web Development &#038; Software Engineering</description>
	<lastBuildDate>Tue, 02 Mar 2010 02:10:53 -0500</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: David Lowenfels</title>
		<link>http://www.missiondata.com/blog/web-development/25/credit-card-type-and-luhn-check-in-ruby/comment-page-1/#comment-18101</link>
		<dc:creator>David Lowenfels</dc:creator>
		<pubDate>Wed, 18 Apr 2007 03:40:15 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.missiondata.com/?p=25#comment-18101</guid>
		<description>here&#039;s my version:

&lt;code&gt;
  def check_digits? num
    odd = true
    num.to_s.gsub(/\D/,&#039;&#039;).reverse.split(&#039;&#039;).map(&amp;:to_i).collect { &#124;d&#124;
      d *= 2 if odd = !odd
      d &gt; 9 ? d - 9 : d
    }.sum % 10 == 0
  end
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>here&#8217;s my version:</p>
<p><code><br />
  def check_digits? num<br />
    odd = true<br />
    num.to_s.gsub(/\D/,'').reverse.split('').map(&amp;:to_i).collect { |d|<br />
      d *= 2 if odd = !odd<br />
      d &gt; 9 ? d - 9 : d<br />
    }.sum % 10 == 0<br />
  end<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Rick Hull</title>
		<link>http://www.missiondata.com/blog/web-development/25/credit-card-type-and-luhn-check-in-ruby/comment-page-1/#comment-2124</link>
		<dc:creator>Rick Hull</dc:creator>
		<pubDate>Tue, 21 Nov 2006 17:26:51 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.missiondata.com/?p=25#comment-2124</guid>
		<description>Here is an even shorter version.  It relies on the fact that the check digit (rightmost) is always considered odd.  So we reverse the number and toggle the odd flag on each accumulation iteration.

&lt;code&gt;
def valid_luhn?(num)
  num = num.to_s.reverse.split(//).map { &#124;d&#124; d.to_i }
  odd = false
  num.inject(0) { &#124;memo, i&#124;
    memo + ((odd = !odd) ? i : (i*2 &gt; 9 ? i*2 - 9 : i*2))
  } % 10 == 0
end
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>Here is an even shorter version.  It relies on the fact that the check digit (rightmost) is always considered odd.  So we reverse the number and toggle the odd flag on each accumulation iteration.</p>
<p><code><br />
def valid_luhn?(num)<br />
  num = num.to_s.reverse.split(//).map { |d| d.to_i }<br />
  odd = false<br />
  num.inject(0) { |memo, i|<br />
    memo + ((odd = !odd) ? i : (i*2 &gt; 9 ? i*2 - 9 : i*2))<br />
  } % 10 == 0<br />
end<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: James</title>
		<link>http://www.missiondata.com/blog/web-development/25/credit-card-type-and-luhn-check-in-ruby/comment-page-1/#comment-1671</link>
		<dc:creator>James</dc:creator>
		<pubDate>Wed, 08 Nov 2006 11:35:58 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.missiondata.com/?p=25#comment-1671</guid>
		<description>Sir,
How can the extraction of name on a credit card be made? your urgent response Sir. Thanks.</description>
		<content:encoded><![CDATA[<p>Sir,<br />
How can the extraction of name on a credit card be made? your urgent response Sir. Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: pvdb</title>
		<link>http://www.missiondata.com/blog/web-development/25/credit-card-type-and-luhn-check-in-ruby/comment-page-1/#comment-1020</link>
		<dc:creator>pvdb</dc:creator>
		<pubDate>Tue, 12 Sep 2006 20:59:19 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.missiondata.com/?p=25#comment-1020</guid>
		<description>In addition to wrighty&#039;s comment:

Another small point, but in all regexps in the ccTypeCheck() method, the lowercase &quot;d&quot;s should all really be &quot;\d&quot; (ie. the digit character class for Ruby regexps)

(I guess the ruby source code on this page gets mangled when it is rendered as HTML by the blogging software, or some such thing, and the backslashes removed in the process)</description>
		<content:encoded><![CDATA[<p>In addition to wrighty&#8217;s comment:</p>
<p>Another small point, but in all regexps in the ccTypeCheck() method, the lowercase &#8220;d&#8221;s should all really be &#8220;\d&#8221; (ie. the digit character class for Ruby regexps)</p>
<p>(I guess the ruby source code on this page gets mangled when it is rendered as HTML by the blogging software, or some such thing, and the backslashes removed in the process)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: wrighty</title>
		<link>http://www.missiondata.com/blog/web-development/25/credit-card-type-and-luhn-check-in-ruby/comment-page-1/#comment-173</link>
		<dc:creator>wrighty</dc:creator>
		<pubDate>Tue, 13 Jun 2006 08:44:03 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.missiondata.com/?p=25#comment-173</guid>
		<description>Just a small point, that regexp only removes capital Ds, it should really be /\D/ instead.</description>
		<content:encoded><![CDATA[<p>Just a small point, that regexp only removes capital Ds, it should really be /\D/ instead.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: darrend</title>
		<link>http://www.missiondata.com/blog/web-development/25/credit-card-type-and-luhn-check-in-ruby/comment-page-1/#comment-7</link>
		<dc:creator>darrend</dc:creator>
		<pubDate>Wed, 15 Mar 2006 14:42:50 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.missiondata.com/?p=25#comment-7</guid>
		<description>A Ruby method without a coroutine? I just can&#039;t stand it...

Seriously though, I tried to give this one a bit more of the Ruby sass...so far it matches the results of the original.

Of course, it is a judgement call which is easier to read and to follow. 

I think my only real claim to an improvement is that now the core of the algorithm is normalized to accept an Enumerable of numeric values; slightly more reusable maybe someday in some context?

&lt;pre&gt;&lt;code&gt;
def luhnother(ccNumber)
  ccNumber = ccNumber.gsub(/D/,&#039;&#039;).split(//).collect { &#124;digit&#124; digit.to_i }
  parity = ccNumber.length % 2

  sum = 0
  ccNumber.each_with_index do &#124;digit,index&#124;
    digit = digit * 2 if index%2==parity
    digit = digit - 9 if digit &gt; 9
    sum = sum + digit
  end

  return (sum%10)==0
end
&lt;/code&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>A Ruby method without a coroutine? I just can&#8217;t stand it&#8230;</p>
<p>Seriously though, I tried to give this one a bit more of the Ruby sass&#8230;so far it matches the results of the original.</p>
<p>Of course, it is a judgement call which is easier to read and to follow. </p>
<p>I think my only real claim to an improvement is that now the core of the algorithm is normalized to accept an Enumerable of numeric values; slightly more reusable maybe someday in some context?</p>
<pre><code>
def luhnother(ccNumber)
  ccNumber = ccNumber.gsub(/D/,'').split(//).collect { |digit| digit.to_i }
  parity = ccNumber.length % 2

  sum = 0
  ccNumber.each_with_index do |digit,index|
    digit = digit * 2 if index%2==parity
    digit = digit - 9 if digit > 9
    sum = sum + digit
  end

  return (sum%10)==0
end
</code></pre>
]]></content:encoded>
	</item>
</channel>
</rss>
