0

Folks,

I have a config/settings.yml file that looks like this:

lab_options: mappings_hash: {can_type: "Tumor::OncologyRun.basic_test"}

then in my model app/models/tumor.rb I wanted to do something like this

def sync_tumor_test
  Settings.lab_options.mappings_hash.to_hash[:can_type](age, demographic)
end

In the above case I want to call the method Tumor::OncologyRun.basic_test with arguments age and demographics. The method Tumor::OncologyRun.basic_test is present in lib/tumor/oncology_run.rb and looks like this:

    module Tumor
     module OncologyRun
      def OncologyRun.basic_test(age, demographics)
       #code here
      end
     end
   end

I know that in ruby method names are strings, so how do I call this with arguments, when I am trying this from the rails console with something like send(Settings.lab_options.mappings_hash.to_hash[:can_type](age, demographic)) I get a NOMethod Error any feedback is much much appreciated, thanks a lot

1 Answer 1

1

In this case you're storing both the object name and the method call in a single string, so first you have to break them up, then use send, like so:

klass, meth = Settings.lab_options.mappings_hash.to_hash[:can_type].split('.')
klass.constantize.send(meth.to_sym, age, demographic)

The above should be equivelant to calling Tumor::OncologyRun.basic_test(age, demographic). The constantize call is necessary to convert from the name of the object into the actual ruby object.

Note - this assumes that basic_test is a class method on OncologyRun. If it's an instance method, you'll need to call new first and then use the send call on the resultant object.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.