Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9168363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T15:36:31+00:00 2026-06-17T15:36:31+00:00

I am new to ruby. I am trying to parse phone numbers from a

  • 0

I am new to ruby. I am trying to parse phone numbers from a CSV file and I did that using the following code. It is working properly.

require 'csv' 
csv_text = File.read('file.csv')
csv = CSV.parse(csv_text, :headers => true)
csv.each do |row|                                      
    puts "Home Phone: #{row['HomePhone']}"
end

What I want is to clean up HomePhone in the following ways.

  1. If phone number has 10 digits, it is good, print it as such.
  2. If phone number has less than 10 digits, print invalid number as “0000000000”
  3. If phone number has 11 digits and first digit is 1, print last 10 digits (remove first 1), else “0000000000”

I don’t know how to do this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T15:36:32+00:00Added an answer on June 17, 2026 at 3:36 pm

    You can get the length of a string with the aptly-named length method:

    string = 'foobar'
    string.length # => 6
    

    You can check if a string starts with another string using start_with?:

    string.start_with?('f') # => true
    

    You can slice a string individual characters using array index notation (square brackets) and a range. A negative index counts from the end of the string. So to return all but the first character:

    string[1..-1] # => 'oobar'
    

    So to do what you are asking you can combine these

    home_phone = row['HomePhone']
    if home_phone.length == 10
      puts home_phone
    elsif home_phone.length == 11 && home_phone.start_with?('1')
      puts home_phone[1..-1]
    else
      puts '0000000000'
    end
    

    Note that this approach assumes that your phone numbers are already strings of digits and you just need to check their length. If you wanted to be more thorough and check for invalid phone numbers containing non-digits, like 123z567890, you might consider a regex approach:

    if match = /^1?(?<number>\d{10})$/.match(row['HomePhone'])
      puts match[:number]
    else
      puts '0000000000'
    end
    

    The components that this regex matches are:

    • ^ – the start of the string
    • 1? – an optional 1
    • (?<number>\d{10}) – 10 digits (ie \d{10}) saved in a group called number
    • $ – the end of the string

    Ruby uses the forward slashes to delimit the regex, and the match method returns an object that we can use to extract the saved 10-digit number.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to parse this website using Ruby and Nokogiri: Here's my code: require
Trying to parse and XLSX file using roo gem in a ruby script. In
I'm new to Ruby. I'm trying to make an app that reads from a
I am new to Ruby and trying to post file from web app to
I'm kinda new to Flex. I have trying to send Hash from Ruby on
RubyParser.new.parse 1+1 s(:call, s(:lit, 1), :+, s(:array, s(:lit, 1))) Above code is from this
I coded a little Ruby script that would parse a remote XML file and
I am trying to run a ruby script that parses a text file with
I am very new to Ruby and I am currently working on site-scraping using
I'm trying to run the following snippet from a brand new rails project in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.