Rubify
Posted by Duane Johnson on rails list:
Here's the modified String class. This one
squeezes non-alphanumeric character sequences down to one underscore
and also makes sure it doesn't start or end with an underscore:
1 2 class String 3 def rubify 4 downcase.gsub(/\W/, ' ').squeeze.strip.gsub(' ', '_') 5 end 6 7 def rubify! 8 replace rubify 9 end 10 end
"!Hell93 o3#$@ the___re , dude".rubify
=> "hel93_o3_the_re_dude"
Luke Redpath suggested the following naming convention:
ID is short for identification the related verb of which is identify.
The appropriate verify_ macro would be verify_identity_of
A more conventional Ruby idiom would be to_id() (like to_s, to_i etc.).
1 2 "My, what a beautiful day!".to_id 3 4 => "my_what_a_beautiful_day"
If thats not clear enough or possibly confusing with the normal use of id in a Rails app, perhaps to_identifier() instead.