Never been to DZone Snippets before?

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 (See related posts)

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 on this post

brandondrew posts on Nov 20, 2007 at 00:16
It fails for me with an error message:

{bash} $ ./String camelize blah_blah
./String:4: undefined method `gem' for main:Object (NoMethodError)

You need to create an account or log in to post comments to this site.


Click here to browse all 5147 code snippets

Related Posts