Note that there are some explanatory texts on larger screens.

plurals
  1. POAdd dynamic methods to the class.methods array
    primarykey
    data
    text
    <p>I am creating dynamic methods using the <strong>method_missing</strong> function. Its all working fine, but the thing i wanna do is, add these methods to <strong>ClassName.methods</strong> array. The code is as follows:</p> <pre><code>module MyObjectStore values = [] temp = {} def self.included(kls) kls.extend ClassMethods end ClassMethods = Module.new do define_method :method_missing do |method,*args,&amp;block| if method =~ /find_by_/ methods &lt;&lt; method add_search_method(method,*args) else values.send "#{method}", &amp;block end end define_method :add_search_method do |method,*args| values.each do |obj| x = obj.send "#{method.to_s.split('_')[-1]}" if x == args[0] p obj end end end end define_method :add_check_attribute do |method,*args| if method.to_s =~ /=$/ temp[method[0..-2]] = args[0] else instance_variable_get("@#{method}") end end define_method :method_missing do |method,*args| add_check_attribute(method,*args) end define_method :save do temp.each {|key,value| instance_variable_set("@#{key}",value)} values &lt;&lt; self end end class C include MyObjectStore end </code></pre> <p>i'm adding the new method to the methods Array. Its being added fine in here. The problem is, when i make a reference of class C, and try to find that newly added method, it returns nothing. i.e.</p> <pre><code>a = C.new a.id = 1 a.name = 'gaurav' a.save C.find_by_name('gaurav') p a.methods.include?(:find_by_name) </code></pre> <p>returns <strong>false</strong></p> <p>Is there a way i could go about doing this?</p> <p>Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload