Easily Convert Namespaced Class Names to Paths in Rails

Easily Convert Namespaced Class Names to Paths in Rails

I came across this nifty little tip in a GoRails episode and wanted to pass it along.

Typically, I'm using #underscore on class names to convert

MyClass

to

my_class

Little did I know, it works on namespaced classes too.

It will convert '::' to '/'. So calling #underscore on

MyApp::MyClass

will result in

my_app/my_class

This obviously comes in handy if you're needing to map class names to paths in Rails.

Sources

Categories: Software Development | Tags: Rails, ActiveSupport, Tips

Portrait photo for Lauren Fackler Lauren Fackler

Lauren's experience includes backend/frontend web development and UI design. She really enjoys working with Ruby and Ruby on Rails and also loves to design, refactor and tidy up code.

Comments


LET US HELP YOU!

We provide a free consultation to discover competitive advantages for your business. Contact us today to schedule an appointment.