Extend multiple classes at once

Posted by Starkers on Stack Overflow See other posts from Stack Overflow or by Starkers
Published on 2014-08-20T10:13:52Z Indexed on 2014/08/20 10:20 UTC
Read the original article Hit count: 176

Filed under:

I want to share a method amongst all Ruby's data object classes. That is to say the following classes:

  • Hash
  • String
  • Number
  • Array

Firstly, any classes I've missed out?

Secondly, how can I share my_method amongst all classes at once?

class Hash
    def my_method
       "hi"
    end
end

class String
    def my_method
       "hi"
    end
end

class Number
    def my_method
       "hi"
    end
end

class Array
    def my_method
       "hi"
    end
end

© Stack Overflow or respective owner

Related posts about ruby