Consider I have a variable class_name. class_name can hold string values like blog, comment etc. based on the value inside class_name I need to create respective object.
If class_name is blog, I need to create a blog object. How can I achieve this without using a switch?
You’d use the
camelizeandconstantizemethods thusly:If you started with
'blog'inclass_name, yourklasswould end up asBlog. If your string is actually'Blog', then:Both
camelizeandconstantizeare Rails-specific but I see the “ruby-on-rails” tag so we’re in Rails-land.