|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
[Note: parts of this message were removed to make it a legal post.]
I've been learning Ruby and have done a couple of the quizes on the rubyquiz.com web site and written a couple of my own. Recently, I was surfing and stumbled on Graham King's credit card generator < http://www.darkcoding.net/projects/credit-card-generator/> He included several versions including PHP, Python and Java, but not Ruby. I thought it would be a good exercise translating it into Ruby and started working on it. What I thought would be an exercise that would take a couple of hours has now taken a few days, and I'm stumped. The problem involves the arrays used to hold the prefixes for the different cards. For some reason the program changes the original prefix so if a prefix is called that was called before it uses the entire randomly selected number. --snip-- American Express ---------------- 345258182650083 377822445136536 3778224451365361 3452581826500839 34525818265008392 Discover -------- 6011448520213005 60114485202130056 601144852021300560 Diners Club / Carte Blanche --------------------------- 30232254153565 30032768147008 38878817351526 --snip-- I think the part that is causing problems is in here: def completed_number(prefix, len) newCcnumber = [] newCcnumber = prefix len = len # generate digits while newCcnumber.length < (len - 1) ran = rand(9) newCcnumber.push(ran).to_s end #reverse number reversedCCnumber = newCcnumber.reverse #calculate sum sum = 0 pos = 0 while pos < (len - 1) odd = (reversedCCnumber[pos].to_i) * 2 if odd > 9 odd -= 9 end sum = sum + odd; if pos != (len - 2) sum = sum + (reversedCCnumber[pos +1]).to_i end pos += 2 end #calculate check digit checkdigit = (((sum/10) + 1) * 10 - sum) % 10 newCcnumber.push(checkdigit) newCcnumber = newCcnumber.to_s+"\n" end I don't really want a full solution to this problem, since I started it as a learning exercise. What I would like is a push in the right direction. What am overlooking? Am I way off on this problem or is it just something simple that I'm missing? amexPrefixList = [ ['3', '4'], ['3', '7'] ] discoverPrefixList = [ ['6', '0', '1', '1'] ] dinersPrefixList = [ ['3', '0', '0'], ['3', '0', '1'], ['3', '0', '2'], ['3', '0', '3'], ['3', '6'], ['3', '8'] ] |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
These lines look VERY suspicious:
newCcnumber = [] newCcnumber = prefix **hint: newCcnumber and prefix are variables which *refer to the same object* after the last statement |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
Wally Terrible wrote:
> I've been learning Ruby and have done a couple of the quizes on the yes newCcnumber = [] newCcnumber = prefix now newCcnumber and prefix refer the same array newCcnumber = [] newCcnumber.concat prefix # append the elements of prefix to newCcnumber newCcnumber and prefix refer to different array tiziano -- Posted via http://www.ruby-forum.com/. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
Thanks. That was it. I suspected I was overlooking something simple. Now
it's working as expected. |
|
![]() |
| Outils de la discussion | |
|
|