# Pushing things Arrays

**URL:** <https://www.ruby-forum.com/t/pushing-things-arrays/125141>\
**Category:** Ruby\
**Created:** [January 6, 2008, 2:41am UTC](https://www.ruby-forum.com/t/pushing-things-arrays/125141 "2008-01-06T02:41:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wally\_Terrible](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/94ad74/32.png) [@Wally\_Terrible](https://www.ruby-forum.com/u/Wally_Terrible)\
**Post date:** [January 6, 2008, 2:41am UTC](https://www.ruby-forum.com/t/pushing-things-arrays/125141/1 "2008-01-06T02:41:20Z")

</div>

## I’ve been learning Ruby and have done a couple of the quizes on the [rubyquiz.com](http://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/](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’] ]

---

<div class="post-metadata">

**Author:** ![Wally\_Terrible](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/94ad74/32.png) [@Wally\_Terrible](https://www.ruby-forum.com/u/Wally_Terrible)\
**Post date:** [January 6, 2008, 6:21am UTC](https://www.ruby-forum.com/t/pushing-things-arrays/125141/2 "2008-01-06T06:21:09Z")

</div>

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

\*\*hint: newCcnumber and prefix are variables which _refer to the same  
object_ after the last statement

---

<div class="post-metadata">

**Author:** ![Wally\_Terrible](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/94ad74/32.png) [@Wally\_Terrible](https://www.ruby-forum.com/u/Wally_Terrible)\
**Post date:** [January 6, 2008, 8:27am UTC](https://www.ruby-forum.com/t/pushing-things-arrays/125141/3 "2008-01-06T08:27:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Wally\_Terrible](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/94ad74/32.png) [@Wally\_Terrible](https://www.ruby-forum.com/u/Wally_Terrible)\
**Post date:** [January 6, 2008, 2:28pm UTC](https://www.ruby-forum.com/t/pushing-things-arrays/125141/4 "2008-01-06T14:28:55Z")

</div>

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