0

I want to dynamically call variables defined in initializers. I have a created a file(brands.rb) in my initializers and it have some variable defined init like,

FEATURED_FASHION_BRAND=["something", "something"]
FEATURED_DIY_BRAND=["something", "something"]

so can I call this variables dynamically, I meant, something like this,

@name= params[:name]
FEATURED_"#{@name}"_BRAND
2
  • BTW, a much nicer way to do this is to make a hash of values in your config. Then you can just say FEATURED_BRANDS[@name] or similar, instead of mucking about with dynamic variable names. Commented Sep 19, 2014 at 13:28
  • And, to go further, you should actually store this stuff in your database: it feels like it would go in a brands table, with a "featured" boolean field. Commented Sep 19, 2014 at 13:30

1 Answer 1

1

you can use constantize for that:

"FEATURED_#{@name}_BRAND".constantize

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

1 Comment

Note that you can use constantize for all sorts of things, for example to turn a string into a class: my_model_name.constantize.find(123)

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.