# Defining an object constructor that look like \[\] or {} ...?

**URL:** <https://www.ruby-forum.com/t/defining-an-object-constructor-that-look-like-or/157949>\
**Category:** Ruby\
**Created:** [January 16, 2009, 8:16pm UTC](https://www.ruby-forum.com/t/defining-an-object-constructor-that-look-like-or/157949 "2009-01-16T20:16:26Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [January 16, 2009, 8:16pm UTC](https://www.ruby-forum.com/t/defining-an-object-constructor-that-look-like-or/157949/1 "2009-01-16T20:16:26Z")

</div>

Hey Guys,

Ruby has built in object constuctors for Array and Hash via [] and {}…  
ie:

an\_array = [1, 2, 3, 6]  
a\_hash = {1=\>3, 2=\>5, ‘a’=\>‘green’}

I would like to do the same for one of my classes… i.e.

my\_object = |2, 3, 5, 7|

should yield the same result as if I had done the following:

my\_object = MyClass.new([2, 3, 5, 7])

Is this possible?

Sonny.

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [January 16, 2009, 8:27pm UTC](https://www.ruby-forum.com/t/defining-an-object-constructor-that-look-like-or/157949/2 "2009-01-16T20:27:57Z")

</div>

> my\_object = MyClass.new([2, 3, 5, 7])

You should be able to use

[] as class method:

```
my_object = MyClass[2, 3, 5, 7]

```

a method in Kernel or similar:

```
my_object = MyClass(2, 3, 5, 7)

```

tom.

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [January 16, 2009, 8:52pm UTC](https://www.ruby-forum.com/t/defining-an-object-constructor-that-look-like-or/157949/3 "2009-01-16T20:52:02Z")

</div>

On Jan 16, 2009, at 1:15 PM, Sonny C. wrote:

> my\_object = |2, 3, 5, 7|
> 
> should yield the same result as if I had done the following:
> 
> my\_object = MyClass.new([2, 3, 5, 7])
> 
> Is this possible?

Offhand, I doubt you can use || for this task, but you can use [].

```
 class MyClass
   def MyClass.[](*arr)
     MyClass.new(arr)
   end

   def initialize(arr)
     @arr = arr
   end
 end

 > x = MyClass[2, 3, 5, 7]
 => #<MyClass:0x7a3b4 @arr=[2, 3, 5, 7]>
```

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [January 17, 2009, 11:05am UTC](https://www.ruby-forum.com/t/defining-an-object-constructor-that-look-like-or/157949/4 "2009-01-17T11:05:34Z")

</div>

On 16.01.2009 20:15, Sonny C. wrote:

> should yield the same result as if I had done the following:
> 
> my\_object = MyClass.new([2, 3, 5, 7])
> 
> Is this possible?

No, because you would have to change Ruby’s syntax. See the other  
postings for alternatives.

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![soroe](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f9ae1b/32.png) [@soroe](https://www.ruby-forum.com/u/soroe)\
**Post date:** [January 17, 2009, 12:10pm UTC](https://www.ruby-forum.com/t/defining-an-object-constructor-that-look-like-or/157949/5 "2009-01-17T12:10:35Z")

</div>

Le 16 janvier 2009 à 20:26, Tom L. a écrit :

> ```
> my_object = MyClass(2, 3, 5, 7)
> 
> ```

A method in Array :

class Array  
def to\_my\_class  
MyClass.new(self)  
end  
end

my\_object = [2, 3, 5, 7].to\_my\_class

Fred
