When you include a gem, a lot of methods are created dynamically by the gem. Is there a way to find all the methods created through def
or define_methods
[sic]?
Asked
Active
Viewed 47 times
1
-
1A method is a method. You could always wrap `define_methods` and capture its invocations. – Dave Newton Jan 25 '16 at 22:07
-
Dave I quite didn't understand what you are mentioning. I am a newbie to ruby. So here is the problem : I am looking at a Rails model and there are some methods which I can see in the model.rb file. Along with that they have included a workflow gem which creates a bunch of methods. So I was trying to see if there is a way to differentiate them. – Shrikar Jan 25 '16 at 22:40
1 Answers
1
You could do something like this:
irb(main):002:0> t = Object.methods; nil
=> nil
irb(main):003:0> require 'rails'
=> true
irb(main):004:0> Object.methods - t
=> [:cattr_reader, :cattr_writer, ..., :silence, :quietly]
# heh, 48 more methods in Object
It isn't really necessary to reverse-engineer Rails, of course. You could just read the docs.

DigitalRoss
- 143,651
- 25
- 248
- 329
-
this is what I tried in a Rails console [36] pry(main)> t = Object.methods; nil => nil [37] pry(main)> Object.methods - t => [] – Shrikar Jan 25 '16 at 22:37
-
Well yeah but you already have rails included when you assign `t.` The point of the expression is to look at `Object` before and after including the gem. – DigitalRoss Jan 25 '16 at 23:01
-
Sure. You could run `irb` or something like `ruby -e 'p Object.methods'` and just copy (to the clipboard) the vanilla `Object` array of symbols using your terminal or IDE. Then paste it in to the rails console and assign *that* to `t.` Keep in mind that not all of the monkey-patching will be inflicted on Object, sometimes a class like String is the victim. – DigitalRoss Jan 25 '16 at 23:09
-
Note that `Object.methods` will return the methods that `Object` responds to, i.e. the methods defined in `Class`, and *not* the ones defined in `Object`. That may or may not be what you want. – Jörg W Mittag Jan 26 '16 at 00:50
-
Yes, see http://stackoverflow.com/questions/4664578/how-do-i-inspect-the-methods-of-a-ruby-object for a discussion of method discovery. – DigitalRoss Jan 26 '16 at 01:03