Forum: Ruby Re: Creating a method like Array(object)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Gennady B. (Guest)
on 2006-05-11 19:54
(Received via mailing list)
What you can do, though, use [] instead of ().

class YourClass
  class << self
    def [](*args)
       p args
    end
  end
end

Then at least YourClass[1, 2, 3] would be available for you.

Gennady.
Sean O. (Guest)
on 2006-05-11 20:32
(Received via mailing list)
On 5/11/06, Gennady B. <removed_email_address@domain.invalid> wrote:
> What you can do, though, use [] instead of ().

Hi Gennady,

You can define a method with the same name as a constant as they're in
separate namespaces, e.g.

class MyClass
  def initialize(*args, &block)
     p args
  end
end

def MyClass(*args, &block)
  MyClass.new(*args, &block)
end

o = MyClass(1,2,3)
p o
#=> #<MyClass:0x2c87910>

As the OP says, Array() is just a method on Kernel.

Regards,

Sean
This topic is locked and can not be replied to.