Sha256: fd3074eb69881679d3c607084b65e20ce8abc4f58ccab1dba1c7e4eb2cf919bd

Contents?: true

Size: 769 Bytes

Versions: 163

Compression:

Stored size: 769 Bytes

Contents

class PhoneNumber
  attr_reader :number
  def initialize(number)
    @number = clean(number)
  end

  def area_code
    number[0..2]
  end

  # technically, the central office (exchange) code
  def exchange_code
    number[3..5]
  end

  def subscriber_number
    number[6..9]
  end

  def to_s
    "(#{area_code}) #{exchange_code}-#{subscriber_number}"
  end

  private

  def clean(number)
    return '0' * 10 if number.match(/[a-zA-Z]/)
    number = number.gsub(/[^0-9]/, '')
    normalize(number)
  end

  def normalize(number)
    if valid?(number)
      number[/(\d{10})\z/, 1]
    else
      '0' * 10
    end
  end

  def valid?(number)
    return true if number.length == 10
    return true if number.length == 11 && number.start_with?('1')
    false
  end
end

Version data entries

163 entries across 163 versions & 1 rubygems

Version Path
trackler-2.0.8.22 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.21 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.20 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.19 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.18 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.17 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.16 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.15 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.14 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.13 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.12 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.11 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.10 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.9 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.8 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.7 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.6 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.5 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.4 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.3 tracks/ruby/exercises/phone-number/example.rb