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.1.1 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.1.0 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.0.10 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.0.9 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.0.8 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.0.7 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.0.6 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.0.5 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.0.4 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.0.3 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.0.2 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.0.1 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.0.0 tracks/ruby/exercises/phone-number/example.rb
trackler-1.0.4.1 tracks/ruby/exercises/phone-number/example.rb
trackler-1.0.4.0 tracks/ruby/exercises/phone-number/example.rb
trackler-1.0.3.0 tracks/ruby/exercises/phone-number/example.rb
trackler-1.0.2.1 tracks/ruby/exercises/phone-number/example.rb
trackler-1.0.2.0 tracks/ruby/exercises/phone-number/example.rb
trackler-1.0.1.2 tracks/ruby/exercises/phone-number/example.rb
trackler-1.0.1.1 tracks/ruby/exercises/phone-number/example.rb