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

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

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