Skip to content

Instantly share code, notes, and snippets.

@ufeanei
Created December 17, 2015 13:49
Show Gist options
  • Select an option

  • Save ufeanei/336309e86e9ef1ff7439 to your computer and use it in GitHub Desktop.

Select an option

Save ufeanei/336309e86e9ef1ff7439 to your computer and use it in GitHub Desktop.

Revisions

  1. ufeanei created this gist Dec 17, 2015.
    21 changes: 21 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,21 @@
    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