Morton Goldberg
6/16/2007 3:45:00 PM
On Jun 16, 2007, at 9:03 AM, John Joyce wrote:
>
> On Jun 15, 2007, at 4:58 PM, Morton Goldberg wrote:
>
>> On Jun 15, 2007, at 4:36 PM, Colin Summers wrote:
>>
>>> So I am stealing all these identities and storing everything in a
>>> big
>>> array of objects stolen_identity, and having the index be social
>>> security numbers:
>>>
>>> ss[34323843] = Stolen_identity.new
>>>
>>> But say I pick up a paycheck stub and it has 3428294. How do I
>>> know if
>>> I already have it? Does
>>> if (exist ss[3428294]) then ... end
>>> work? How can I see if a variable exists?
>>>
>>> Thanks.
>>>
>>> (please include your social security with your answer)
>>
>> Using a hash is the way to go here. You've already been advised to
>> do that. But here is an example that may give you some additional
>> insight.
>>
>> <code>
>> class Identity
>> def initialize(ss)
>> @ss = ss
>> end
>> def process
>> puts "#{@ss}: I've been stolen!"
>> end
>> end
>>
>> Stolen = {}
>>
>> ss1 = '123-12-1234'
>> ss2 = '789-78-7890'
>> Stolen[ss1] = Identity.new(ss1)
>>
>> # One way
>> Stolen[ss1].process if Stolen[ss1] # process runs
>> Stolen[ss2].process if Stolen[ss2] # process does not run
>>
>> # Another way
>> Stolen[ss1].process rescue nil # process runs
>> Stolen[ss2].process rescue nil # process does not run
>> </code>
>>
>> There are many other ways, but they all rely on same thing: that
>> Stolen[ss2] returns nil and nil is treated as false in boolean
>> expressions.
>>
>> Regards, Morton
>>
> An array could indeed work. Think about the structure of a SSN:
> 377-23-4736 (not a real one)
> That looks like a multi-dimensional array to me.
> each part is limited: 3 digits, 2 digits, 4 digits.
> Much like IPv4 this can produce a lot of distinct numbers.
> These smaller parts are easier to process, possibly less overhead.
I don't doubt that Identity objects could be indexed into some kind
of array, but I do think the hash approach is simpler and easier to
implement. I'd be happy to be convinced otherwise. How about posting
some code to prove your point?
Regards, Morton