2

After creating the loop to check that the phone number is 10 characters I beleive the phone issue is now resolved. Now I'm working with checking the email address, making sure it outputs correctly, and making sure 2 names are entered by the user. Having issues getting the email address to validate and output.

    def fullname
        "#{first_name} #{last_name}"
    end

    puts "Enter your first and last name (John Doe): "
    name=gets.to_s
    names=name.split(" ", 2)
    puts "Enter your email address (joe@info.com): "
    email=gets
    puts "Enter your phone number including area code (numbers only): "
    number=gets.to_i

     def valid_email(email)
        email=email.to_s
        email="user@company_name.com"
        loop do
          if (email=/\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i)
          break
          else
          puts "Invalid email address entered. Please try again. "
          end
        end
    end

    def phone_number(number)
        number        = number.to_s

        area_code     = number.length == 10 ? "(#{number[0..2]}) " : '' 

        office_code   = number[-7..-5] 
        specific_line = number[-4..-1]
    loop do
        if number =10
        break   
            else
            puts "Invalid phone number entered. Please try again."
            end
        end
        "#{area_code}#{office_code}-#{specific_line}"
    end

    puts names
    puts valid_email(email)
    puts phone_number(number)
Nate_LADE
  • 59
  • 10
  • If there is a simpler way to output the phone number in the format of (123) 123-1234 please let me know. Thanks! – Nate_LADE May 28 '16 at 01:37
  • 1
    Your immediate error comes from the fact that 'symbolize_keys' is not defined. The method 'symbolize_keys' comes from rails, and it looks like you're trying to do vanilla ruby. Simplest way to avoid relying on that method would be to pass in your options as symbols to begin with. – Some Guy May 28 '16 at 01:50
  • [This blog entry](https://davidcel.is/posts/stop-validating-email-addresses-with-regex/) may be of interest. – Cary Swoveland May 28 '16 at 18:29
  • 1
    Completely changing the meaning of your question is undesirable behaviour here. If you have a new question, then ask another question. – Frederick Cheung May 29 '16 at 15:06

1 Answers1

1

As Some Guy mentioned in a comment to your question, your error comes from using the undefined method symbolize_keys. If you wanted you could rewrite the method to use strings instead of symbols when accessing the array.

However, I don't see a place in your code where you ever call number_to_phone and pass in the options hash which would require setup in finding the relevant data. It's probably better to remove that code entirely and figure out how to get the area code in the actual method.

If there is a simpler way to output the phone number in the format of (123) 123-1234 please let me know.

You could try breaking the number up into pieces and then sticking it back together. The first three numbers are the area code (if it exists). The next (or first) three numbers are the office code. And the last three numbers are the specific line. The easiest way to do this would be to start at the end of the numbers, and grab the appropriate chunks, and then return everything together.

def phone_number(number)
  # Strings are easier to work with
  number        = number.to_s

  # Raise an error if the length is invalid
  raise "This is not a phone number" if number.length != 7 || number.length != 10

  # Set the area code if it exists, and add the parens
  area_code     = number.length == 10 ? "(#{number[0..2]}) " : '' 

  # Set the office code and line number from the end of the string because there may not be an area code
  office_code   = number[-7..-5] 
  specific_line = number[-4..-1]

  # Return the final product
  "#{area_code}#{office_code}-#{specific_line}"
end

To check if an email is valid:

VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/
def valid_email(email)
  raise "This is not an email address" unless email =~ VALID_EMAIL_REGEX
end

Edited: The phone_number method and added the valid_email method.

Dbz
  • 2,721
  • 4
  • 35
  • 53
  • This formats it correctly, Now I need to use a loop to check that the phone number entered is also valid. As well as a email address is entered and valid. – Nate_LADE May 28 '16 at 01:59
  • If your definition for valid number is that it has 7 or 10 digits, then you can make a test for that at the beginning of the`phone_number` method. To check that the email address entered it valid, I would use a regular expression. – Dbz May 28 '16 at 02:05
  • I would look at [this stackoverflow](http://stackoverflow.com/questions/22993545/ruby-email-validation-with-regex) to see which email regex to use – Dbz May 28 '16 at 02:06
  • How do I add a regex to this program? I have never used regex's before. – Nate_LADE Jun 01 '16 at 00:06
  • I'm still struggling to get the user input to validate and display as: User: John Doe Phone: (555) 555-5555 Email: johndoe@aol.com – Nate_LADE Jun 01 '16 at 00:44
  • Hey @Natela I have updated my answer to include the checking the email with a regular expression. If you want to display everything at the end a certain way, it might be best to make a method with the sole purpose of outputting the text correctly. – Dbz Jun 02 '16 at 05:23
  • I also updated the `phone_number` method to throw an exception on an invalid length number – Dbz Jun 02 '16 at 05:24