# Howto ? :: Person is a customer

**URL:** <https://www.ruby-forum.com/t/howto-person-is-a-customer/100298>\
**Category:** Ruby\
**Created:** [July 28, 2006, 7:41am UTC](https://www.ruby-forum.com/t/howto-person-is-a-customer/100298 "2006-07-28T07:41:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Squeak\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@Squeak\_S](https://www.ruby-forum.com/u/Squeak_S)\
**Post date:** [July 28, 2006, 7:41am UTC](https://www.ruby-forum.com/t/howto-person-is-a-customer/100298/1 "2006-07-28T07:41:21Z")

</div>

Hi,

Beginner question probably but I do not know how to do it correctly.

I will read informations from a file and create an object.  
During my load I do not know if it is a employe or a customer so I  
create a person object.  
After the object is created I will know which type it is.

After I know I want to change the type of my object.

How will you do it ?

Best Regards  
W.

---

<div class="post-metadata">

**Author:** ![Squeak\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@Squeak\_S](https://www.ruby-forum.com/u/Squeak_S)\
**Post date:** [July 28, 2006, 8:45am UTC](https://www.ruby-forum.com/t/howto-person-is-a-customer/100298/2 "2006-07-28T08:45:34Z")

</div>

On Jul 28, 2006, at 12:39 AM, Squeak S. wrote:

> How will you do it ?
> 
> Best Regards  
> W.

How about making Person#to\_employee and Person#to\_customer methods?  
That would be very Ruby-like, would it not?

However, I think I would just find a way to wait until I know before  
instantiating it, then passing all necessary data into the  
appropriate .new method when I do.

- Jake McArthur

---

<div class="post-metadata">

**Author:** ![Squeak\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@Squeak\_S](https://www.ruby-forum.com/u/Squeak_S)\
**Post date:** [July 28, 2006, 9:23am UTC](https://www.ruby-forum.com/t/howto-person-is-a-customer/100298/3 "2006-07-28T09:23:02Z")

</div>

Squeak S. wrote:

> Hi,
> 
> Beginner question probably but I do not know how to do it correctly.
> 
> I will read informations from a file and create an object.  
> During my load I do not know if it is a employe or a customer so I  
> create a person object.  
> After the object is created I will know which type it is.
> 
> After I know I want to change the type of my object.

“Type”? What exactly do you mean by ‘type’?

(Check out the list archives for threads on duck typing and static  
typing to see how the notion of ‘type’ has been discussed. Might give  
you some ideas.)

–  
James B.

“I was born not knowing and have had only a little  
time to change that here and there.”

- Richard P. Feynman

---

<div class="post-metadata">

**Author:** ![Squeak\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/5f9b8f/32.png) [@Squeak\_S](https://www.ruby-forum.com/u/Squeak_S)\
**Post date:** [July 28, 2006, 7:57pm UTC](https://www.ruby-forum.com/t/howto-person-is-a-customer/100298/4 "2006-07-28T19:57:58Z")

</div>

> > I will read informations from a file and create an object.  
> > During my load I do not know if it is a employe or a customer so I  
> > create a person object.  
> > After the object is created I will know which type it is.
> > 
> > After I know I want to change the type of my object.
> 
> “Type”? What exactly do you mean by ‘type’?

If you mean class, you can do this (untested in irb)

class Person  
def self.new( param, data )  
if param==1  
Employee.new(data)  
else  
Customer.new(data)  
end  
end  
end

Now Person.new(param, data) will return an Employee or a Customer  
depending  
on the param it is passed. Adjust as needed in terms of inspecting the  
input.

I consider this needless trickery, but have used it before because it  
amuses  
me. The other responses have more actual software engineering merit.

Cheers,

ben
