[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Pushing things Arrays

Wally Terrible

1/6/2008 1:40:00 AM

[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-gene... 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'] ]

5 Answers

Paul Stickney

1/6/2008 5:20:00 AM

0

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



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

Tiziano Merzi

1/6/2008 7:27:00 AM

0

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-....

Wally Terrible

1/6/2008 1:28:00 PM

0

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

Pokok'e Joged

2/13/2014 2:26:00 PM

0

SuperSPY <superb.sg@gmail.com> wrote in news:6771ee47-4295-4572-b77b-
40b6a9769de9@googlegroups.com:

> My spy drone tells me that you are "TheInquirer" with a new name.
> Am I right or am I right?

I can assure you he is not

I happened to know both "pretty well"

Pokok'e Joged

2/13/2014 2:28:00 PM

0

"AleXX" <circle?@cycle?.net> wrote in news:ldi1ii$md7$1@dont-email.me:

> But China have also invested a lot of time and money to
> produce superior missiles to shoot down these drones.
>


while the purpose is noble,

it somehow would still be tagged "made in China"