Pushing things Arrays

W

Wally Terrible

[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'] ]
 
P

Paul Stickney

These lines look VERY suspicious:
newCcnumber = []
newCcnumber = prefix



**hint: newCcnumber and prefix are variables which *refer to the same
object* after the last statement
 
T

Tiziano Merzi

Wally said:
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
 
W

Wally T Terrible

Thanks. That was it. I suspected I was overlooking something simple. Now
it's working as expected.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top