Last active
December 23, 2015 22:19
-
-
Save MOZGIII/6702792 to your computer and use it in GitHub Desktop.
LettersMixer - simple letters mixing
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
# encoding: UTF-8 | |
module LettersMixer | |
REGEXP = /[A-zА-я]{2,}/ | |
DEFAULT_MODE = :shuffle | |
module Modes | |
def self.full_shuffle(word, options = {}) | |
word.split("").shuffle(options).join | |
end | |
def self.full_sort(word, options = {}) | |
load_active_support | |
word.split("").sort{|a,b| a.mb_chars.downcase.to_s <=> b.mb_chars.downcase.to_s}.join | |
end | |
def self.shuffle(word, options = {}) | |
return if word.length <= 2 | |
word[1..-2] = full_shuffle(word[1..-2], options) | |
word | |
end | |
def self.sort(word, options = {}) | |
return if word.length <= 2 | |
word[1..-2] = full_sort(word[1..-2], options) | |
word | |
end | |
private | |
# Used for downcasing | |
def self.load_active_support | |
$active_support_loaded ||= require "active_support/core_ext" | |
end | |
end | |
def self.rebuild_text(text, options = {}) | |
options[:regexp] ||= REGEXP | |
options[:mode] ||= DEFAULT_MODE | |
options[:mode] = options[:mode].to_sym | |
raise "Unknown mode" unless Modes.respond_to?(options[:mode]) | |
text.gsub(options[:regexp]) do |word| | |
new_word = Modes.send(options[:mode], word) | |
new_word || word | |
end | |
end | |
end | |
if $0 == __FILE__ | |
print LettersMixer.rebuild_text(File.read(ARGV[0], :encoding => 'utf-8'), mode: ARGV[1]) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment