Skip to content

Instantly share code, notes, and snippets.

@ufeanei
Created December 17, 2015 13:49
Show Gist options
  • Save ufeanei/336309e86e9ef1ff7439 to your computer and use it in GitHub Desktop.
Save ufeanei/336309e86e9ef1ff7439 to your computer and use it in GitHub Desktop.
octal challenge
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