Skip to content

Instantly share code, notes, and snippets.

@marcinbunsch
Created March 7, 2012 17:11
Show Gist options
  • 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
@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