def to_ean ean = '978' + @isbn[0..8] sum = 0 0.step( 10, 2 ) { |n| sum += ean[n..n].to_i sum += ean[n+1..n+1].to_i * 3 } ean += "#{ ( 10 * ( ( sum / 10 ) + 1 ) - sum ) % 10 }" return ean end
Example 2: Ruby version of Example 1.
Dr. Dobb's is part of the Informa Tech Division of Informa PLC
This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.
def to_ean ean = '978' + @isbn[0..8] sum = 0 0.step( 10, 2 ) { |n| sum += ean[n..n].to_i sum += ean[n+1..n+1].to_i * 3 } ean += "#{ ( 10 * ( ( sum / 10 ) + 1 ) - sum ) % 10 }" return ean end
To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy. | |
This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!
Download the latest issue today. >>