lib/rufus/mnemo.rb in rufus-mnemo-1.1.1 vs lib/rufus/mnemo.rb in rufus-mnemo-1.2.0

- old
+ new

@@ -1,7 +1,7 @@ #-- -# Copyright (c) 2007-2009, John Mettraux, jmettraux@gmail.com +# Copyright (c) 2007-2010, John Mettraux, jmettraux@gmail.com # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell @@ -66,11 +66,11 @@ # # might be useful when used from some scripts. # module Mnemo - VERSION = '1.1.1' + VERSION = '1.2.0' V = %w[ a e i o u ] C = %w[ b d g h j k m n p r s t z ] SYL = [] @@ -99,30 +99,30 @@ NEG = 'wi' NEGATIVE = /^#{NEG}(.+)$/ # Turns the given integer into a Mnemo word. # - def Mnemo.from_integer (integer) + def self.from_integer (integer) return "#{NEG}#{from_integer(-integer)}" if integer < 0 s = from_i(integer) to_special(s) end # Turns the given Mnemo word to its equivalent integer. # - def Mnemo.to_integer (string) + def self.to_integer (string) s = from_special(string) to_i(s) end # Turns a simple syllable into the equivalent number. # For example Mnemo::to_number("fu") will yield 19. # - def Mnemo.to_number (syllable) + def self.to_number (syllable) SYL.each_with_index do |s, index| return index if syllable == s end raise "did not find syllable '#{syllable}'" @@ -130,22 +130,22 @@ # Given a Mnemo 'word', will split into its list of syllables. # For example, "tsunashima" will be split into # [ "tsu", "na", "shi", "ma" ] # - def Mnemo.split (word) + def self.split (word) word = from_special(word) a = string_split(word) a_to_special(a) end # Returns if the string is a Mnemo word, like "fugu" or # "toriyamanobashi". # - def Mnemo.is_mnemo_word (string) + def self.is_mnemo_word (string) begin to_integer(string) true rescue #Exception => e @@ -153,43 +153,35 @@ end end private - def Mnemo.string_split (s, result=[]) + def self.string_split (s, result=[]) return result if s.length < 1 result << s[0, 2] string_split(s[2..-1], result) end - def Mnemo.a_to_special (a) + def self.a_to_special (a) - a.collect do |syl| - SPECIAL.each do |a, b| - if syl == a - syl = b - break - end - end - syl - end + a.collect { |syl| SPECIAL.find { |aa, bb| syl == bb } || syl } end - def Mnemo.to_special (s) + def self.to_special (s) - SPECIAL.inject(s) { |s, (a, b)| s.gsub(a, b) } + SPECIAL.inject(s) { |ss, (a, b)| ss.gsub(a, b) } end - def Mnemo.from_special (s) + def self.from_special (s) - SPECIAL.inject(s) { |s, (a, b)| s.gsub(b, a) } + SPECIAL.inject(s) { |ss, (a, b)| ss.gsub(b, a) } end - def Mnemo.from_i (integer) + def self.from_i (integer) return '' if integer == 0 mod = integer % SYL.length rest = integer / SYL.length @@ -199,10 +191,10 @@ # mathn prevention from_i(rest) + SYL[mod] end - def Mnemo.to_i (s) + def self.to_i (s) return 0 if s.length == 0 if m = s.match(NEGATIVE) return -1 * to_i(m[1])