I would like to create a general-purpose string manipulation class that can be used in models, views, and controllers in my Rails application.
I'm currently trying to put a module in my lib directory, and I'm just trying to access a function in the rails console to test it. I tried many methods from similar questions, but I can't get it to work.
In my lib / filenames.rb file:
module Filenames def sanitize_filename(filename) # Replace any non-letter or non-number character with a space filename.gsub!(/[^A-Za-z0-9]+/, ' ') #remove spaces from beginning and end filename.strip! #replaces spaces with hyphens filename.gsub!(/\ +/, '-') end module_function :sanitize_filename end
When I try to call sanitize_filename ("some string"), I get a method error. When I try to call Filenames.sanitize_filename ("some string"), I get an uninitialized persistent error. And when I try to include '/ lib / filenames', I get a loading error.
Thanks!
ruby ruby-on-rails ruby-on-rails-3
corbin
source share