-
-
Save gotmayonase/1996344 to your computer and use it in GitHub Desktop.
Loch Ness Monster Case implementation in Ruby
This file contains hidden or 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 | |
it "works with the alias!" do | |
"Monsters::LochNessMonsterCase".nessy_case.should == 'monSTers/lOCh_nESs_moNSter_cASe' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment