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.1.0.7 tracks/ruby/exercises/phone-number/example.rb
trackler-2.1.0.6 tracks/ruby/exercises/phone-number/example.rb
trackler-2.1.0.5 tracks/ruby/exercises/phone-number/example.rb
trackler-2.1.0.4 tracks/ruby/exercises/phone-number/example.rb
trackler-2.1.0.3 tracks/ruby/exercises/phone-number/example.rb
trackler-2.1.0.2 tracks/ruby/exercises/phone-number/example.rb
trackler-2.1.0.1 tracks/ruby/exercises/phone-number/example.rb
trackler-2.1.0.0 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.55 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.54 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.53 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.52 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.51 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.50 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.49 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.48 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.47 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.46 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.45 tracks/ruby/exercises/phone-number/example.rb
trackler-2.0.8.44 tracks/ruby/exercises/phone-number/example.rb