Last active
March 12, 2016 21:01
-
-
Save holdenhinkle/1e53514fb0f8e6de8e5f to your computer and use it in GitHub Desktop.
Trinary
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
class Trinary | |
attr_reader :number | |
def initialize(value) | |
@number = value | |
end | |
def to_decimal | |
return 0 if number.match(/[^0-2]/) | |
converted_num = 0 | |
number.split('').reverse.each_with_index do |num, index| | |
converted_num += num.to_i * 3 ** index | |
end | |
converted_num | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment