batkuip
batkuip

Reputation: 1478

Extending Array hides initializer?

I'm using ruby 1.8.7

my_array = Array.new(5, "A")

works fine. However if I extend the Array class like this:

class Array
  def my_function
    self
  end
end

then the initializer no longer works and returns

wrong number of arguments (2 for 0)

Why is this and how do I fix it?

Upvotes: 1

Views: 89

Answers (1)

Alex D
Alex D

Reputation: 30455

Are you doing this inside a Module? If so, you are defining a separate Array class, rather than extending Array from the standard library.

Make sure your extension is at the top level, or else write:

class ::Array

(By the way, is there a good reason why you want to use Ruby 1.8.7? If not, I would recommend using an up-to-date version -- Ruby has improved over the years.)

Upvotes: 3

Related Questions