Created
March 7, 2012 17:11
-
-
Save marcinbunsch/1994431 to your computer and use it in GitHub Desktop.
Loch Ness Monster Case implementation in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
nessy_case - perfect! brought it in here :)
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
+1, though I'd like to see an alias to nessy_case -- I've forked to support this: https://gist.github.com/1996344