6

I have a new requirement on Array object. So I need to add my own method to built-in Array class.

How do I add a new method so that whatever Array object I create, it will also have my instance method?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
user2562153
  • 317
  • 1
  • 3
  • 12
  • 1
    Google "ruby open classes" – Sergio Tulentsev Jul 25 '13 at 13:04
  • 2
    It's called [monkey patching](http://en.wikipedia.org/wiki/Monkey_patch) and it's done all the time. There's nothing special about the core classes that prevents you from adding methods to them. – tadman Jul 25 '13 at 15:20

2 Answers2

14

Use Ruby Open Classes:

class Array
  def mymethod
    #implementation
  end
end
Marek Lipka
  • 50,622
  • 7
  • 87
  • 91
  • If I do that my previous array objects don't get built in methods?? – user2562153 Jul 25 '13 at 13:30
  • They were objects of built-in Array and now became object of my new class called Array. So only method available to that object is the newly added instance method. For eg. uniq method is no longer working. When I do this object.methods all I see are my own module methods (where I defined the above class Array) and some methods of the module where the Array is originally defined. How do I get around? – user2562153 Jul 25 '13 at 13:34
  • @user2562153 It's not your new class. It's still the same `Array` class, with all build in `Array` methods. – Marek Lipka Jul 25 '13 at 13:36
  • obj.class => # Class: X::Y::Z::Array, obj.methods => # Methods: ["==", "===", "=~", "__id__", "__send__", "class", "clone", "cy_rel_require", "display", "dup", "eql?", "equal?", "extend", "freeze", "frozen?", "hash", "id", "inspect", "instance_eval", "instance_of?", "instance_variable_defined?", "instance_variable_get", "instance_variable_set", "instance_variables", "is_a?", "kind_of?", "method", "methods", "my_contains_all?", "nil?", "object_id", "private_methods", "protected_methods", "public_methods", "respond_to?", "send", "singleton_methods", "taint", "tainted?", "to_a", "to_s", ... – user2562153 Jul 25 '13 at 13:37
  • I am not able to get uniq method here. and others like each, each_with_index, etc. All I could see that the my methods defined in the same module where this class is also declared and some built-in like I reported above. – user2562153 Jul 25 '13 at 13:38
  • I had a method A; method B; def someMethod; end; class Array def myMethodToArry; end; end #class Arry; end #module B end #module A. And in other place I mixin A::B, Now in that class, where I mixed in the module, all original objects are now objects of A::B::Array and could only see limited instane mehtods. – user2562153 Jul 25 '13 at 13:43
  • 1
    Then you're doing something wrong, but, because you have NOT included the code you're using in your question, we can't help fix it. That is why you have to include the code. This is a simple question and problem but YOU have to help us help you. As is, people will vote to close your question for lack of useful information. – the Tin Man Jul 25 '13 at 13:46
  • @user2562153 is what? – Marek Lipka Jul 25 '13 at 17:09
8

The other answers basically show you can add a method to the class by redefining the class, just to add to that, an example could be like this:

class Array
    def third
        size > 2 ? self[2] : nil
    end
end

a = [1, 2, 3, 4, 5]

puts a.third
jsenk
  • 111
  • 1