Last active
April 17, 2020 22:52
-
-
Save novohispano/2e10a0349bd5bbb1cc93ddaaa7176152 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Phone | |
attr_reader :number | |
def initialize(input) | |
@number = validate_phone(input) | |
end | |
def area_code | |
number[0..2] | |
end | |
def to_s | |
"(#{area_code}) #{number[3..5]}-#{number[6..10]}" | |
end | |
private | |
def validate_phone(number) | |
phone = clean(number) | |
if phone.length == 10 | |
phone | |
elsif phone.length == 11 && phone[0] == "1" | |
phone[1..10] | |
else | |
invalid_phone | |
end | |
end | |
def clean(phone) | |
phone.gsub(/\D/, '') | |
end | |
def invalid_phone | |
"0000000000" | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Robot | |
attr_reader :name | |
def initialize | |
generate_name | |
end | |
def reset | |
generate_name | |
end | |
private | |
def generate_name | |
@name = "" | |
2.times { @name << letter } | |
3.times { @name << digit } | |
end | |
def letter | |
rand(65..90) | |
end | |
def digit | |
rand(0..9).to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment