DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
Unix Shell Script Providing Ruby On Rails Enhanced String Methods
Assuming you have Rails installed as a gem, the following Unix shell script (which I named String, but you can name anything you want when you save the following into a file) allows you to call Ruby String methods (including, importantly, the methods that the Rails ActiveSupport extensions add) on an arbitrary number of arguments, and it will print out the results, e.g.,
$ String camelize snake_case_example another_snake_case_example
The output is:
SnakeCaseExample
AnotherSnakeCaseExample
#!/usr/bin/env ruby
if ARGV.size > 1 then
gem 'activesupport'
require 'active_support/core_ext/string/inflections'
class String
include ActiveSupport::CoreExtensions::String::Inflections
end
command = ARGV.shift
ARGV.each { |argument|
puts argument.send( command )
}
else
# Print usage information
puts "Usage: #{File.basename( __FILE__ )} <command> <argument_1> [<argument_2> ...]"
end





Comments
Brandon Zylstra replied on Tue, 2007/11/20 - 12:51am