Skip to content

Instantly share code, notes, and snippets.

@gotmayonase
Forked from marcinbunsch/lochness.rb
Created March 7, 2012 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gotmayonase/1996344 to your computer and use it in GitHub Desktop.
Save gotmayonase/1996344 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
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