Skip to content

Instantly share code, notes, and snippets.

@gavinmcgimpsey
Last active December 19, 2015 06:25
Show Gist options
  • Save gavinmcgimpsey/68579c41f1eb52bfbe6b to your computer and use it in GitHub Desktop.
Save gavinmcgimpsey/68579c41f1eb52bfbe6b to your computer and use it in GitHub Desktop.
class Octal
def initialize(str)
@digits = str.chars
end
def to_decimal
return 0 if @digits.any? { |digit| digit =~ /[^0-7]/ }
@digits
.map(&:to_i)
.reverse
.each_with_index
.map { |digit, index| digit * (8 ** index) }
.inject(:+)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment