Search code examples
ruby-on-railsrubyruby-on-rails-3.1metaprogramming

meta programming to include module


I'm using ruby on rails and I currently have code

if level == "Grad"
  include Grad
else
  include Undergrad

Where level is a string that say either "Grad" or "Undergrad"

I want to use meta programming to include the correct module without the if statement

something like this.

send :include, level

How do I do this. What is the best way to do this?


Solution

  • include "#{level}".constantize. Does this work for you?