Skip to content

Instantly share code, notes, and snippets.

@marcinbunsch
Created March 7, 2012 17:11
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save marcinbunsch/1994431 to your computer and use it in GitHub Desktop.
Save marcinbunsch/1994431 to your computer and use it in GitHub Desktop.
Loch Ness Monster Case implementation in Ruby
class String
def loch_ness_monster_case
self.split('::').collect do |section|
section.gsub(/([^\/])([A-Z])/, '\1_\2').downcase.split(/_/).collect { |part|
chars = part.split('')
half = chars.length/2
chars[half].upcase!
chars[half - 1].upcase!
chars.join('')
}.join('_')
end.join('/')
end
alias :nessy_case :loch_ness_monster_case
end
if __FILE__ == $0
require "rspec"
require 'rspec/autorun'
describe "String#loch_ness_monster_case" do
it "works!" do
"LochNessMonsterCase".loch_ness_monster_case.should == 'lOCh_nESs_moNSter_cASe'
"loch_ness_monster_case".loch_ness_monster_case.should == 'lOCh_nESs_moNSter_cASe'
end
it "works with modules!" do
"Monsters::LochNessMonsterCase".loch_ness_monster_case.should == 'monSTers/lOCh_nESs_moNSter_cASe'
end
end
end
@elliottkember
Copy link

@gotmayonase
Copy link

+1, though I'd like to see an alias to nessy_case -- I've forked to support this: https://gist.github.com/1996344

@marcinbunsch
Copy link
Author

nessy_case - perfect! brought it in here :)

@amacneil
Copy link

amacneil commented Mar 7, 2012

Can we have a PHP implementation please? It would be a perfect addition to the many different cases already in use :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment