Created
December 17, 2015 13:49
-
-
Save ufeanei/336309e86e9ef1ff7439 to your computer and use it in GitHub Desktop.
octal challenge
This file contains 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 Octal | |
attr_accessor :octnum | |
def initialize(num) | |
@octnum = num | |
end | |
def not_valid? | |
octnum == '0' || octnum =~ /[A-Fa-f8-9]/ | |
end | |
def to_decimal | |
sum = 0 | |
return sum if not_valid? | |
oct_reverse = octnum.reverse | |
for i in 0...oct_reverse.size | |
sum += oct_reverse[i].to_i* 8**i | |
end | |
sum | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment