How to create an array of methods in rails -


def id_attachment_require_upload?     !object.id_attachment? end  ...  def work_attachment_require_upload?     !object.work_attachment? end 

i want make below.

array = %w(id address work) array.each |a|     def #{a}_attachment_require_upload?         !object.#{a}_attachment?     end end 

is there way me create array of methods automatically in rails save me redundant work.

arup's answer looks it's way go i'm not sure if object.#{a}_attachment? work. if does, learned new today. can use public_send.

array = %w[id address work] array.each |a|   define_method "#{a}_attachment_require_upload?"     !object.public_send("#{a}_attachment?")   end end 

Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -