Skip to content

Instantly share code, notes, and snippets.

@adamzaninovich
Last active December 14, 2015 03:39
Show Gist options
  • Save adamzaninovich/5022970 to your computer and use it in GitHub Desktop.
Save adamzaninovich/5022970 to your computer and use it in GitHub Desktop.
A ruby class to calculate English Gematria. This example uses a mispar hechrachi style correspondence table, but that can easily be swapped out for another table. Class supports raw conversion to number, mapping (breakdown of numbers), and reduction to a single digit (mispar katan mispari). ***Scroll down to see it in action.
# Ruby class to calculate English Gematria
class Gematria < Struct.new(:text)
CORRESPONDENCE_TABLE = { # applies `mispar hechrachi` method to English alphabet (http://www.inner.org/gematria/fourways.php)
a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9,
j: 10, k: 20, l: 30, m: 40, n: 50, o: 60, p: 70, q: 80, r: 90,
s: 100, t: 200, u: 300, v: 400, w: 500, x: 600, y: 700, z: 800
}
def mapped
text.each_char.map { |c| lookup_char c }
end
def converted
mapped.inject(:+)
end
def reduced # delegates to private recursive function
do_reduction_on converted
end
private
def do_reduction_on(number)
if number < 10
number
else
do_reduction_on number.to_s.each_char.map(&:to_i).inject(:+)
end
end
def lookup_char(char) # find lowercase letter, characters not in the table have no value
CORRESPONDENCE_TABLE.fetch(char.downcase.to_sym, 0)
end
end
# Example usage
name = Gematria.new("Adam")
name.converted # => 46
name.mapped.join(" + ") # => "1 + 4 + 1 + 40"
name.reduced # => 1
gematria = Gematria.new("Gematria is fun!")
gematria.converted # => 818
gematria.mapped # => [7, 5, 40, 1, 200, 90, 9, 1, 0, 9, 100, 0, 6, 300, 50, 0]
alphabet = Gematria.new("abcdefghijklmnopqrstuvwxyz")
alphabet.converted == 4095 # => true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment