Skip to content

Instantly share code, notes, and snippets.

@mkweick
Last active March 12, 2016 12:31
Show Gist options
  • Save mkweick/1990f4179b060c648fbc to your computer and use it in GitHub Desktop.
Save mkweick/1990f4179b060c648fbc to your computer and use it in GitHub Desktop.
Trinary
class Trinary
def initialize(num)
num = '0' unless num =~ /^[012]+$/
@num = num.reverse.chars.collect(&:to_i)
end
def to_decimal
@num.map.with_index { |num, idx| num * (3 ** idx) }.inject(:+)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment