2

A ruby array can be initialized using:

irb(main):01:0> Array[1,2,3]
=> [1, 2, 3]

How can I emulate this behavior in my own class?

2 Answers 2

4

You can implement the public [](*args) class method.

Sign up to request clarification or add additional context in comments.

Comments

1
class Foo
  def self.[] *args
    new *args
  end
  # ...
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.