How does this ruby error handling module code work

Posted by Michael Durrant on Programmers See other posts from Programmers or by Michael Durrant
Published on 2012-11-24T16:05:28Z Indexed on 2012/11/24 17:17 UTC
Read the original article Hit count: 277

Filed under:
|

Trying to get a better handle on ruby exception handling. I have this code (from a book):

def err_with_msg(pattern)
  m = Module.new
  (class << m; self; end).instance_eval do
    define_method(:===) do |e|
      pattern === e.msg
    end
  end
  m
end

So ok this is a method. We're creating a new Module. I think of module as mix-ins. Not sure what it's doing here. Not we add the module to the class. Fair enough. Then we have self on its own. What that for? I guess we have a little anonymouse method this is just about self. hmmm ok, now for each of the above, check the pattern match. but for each, I thought the above for for a new Module, did the module get to use instance's by being included?

A better explanation of what's going on here would be most helpful.

© Programmers or respective owner

Related posts about ruby

Related posts about exception-handling