how to convert 3012944070 to (301) 294-4070 using rails [duplicate] - ruby-on-rails

This question already has answers here:
How to format this international phone number in Rails?
(4 answers)
Closed 8 years ago.
I need to convert the phone number to international phone number example
3012944070 Output will be like this (301) 294-4070

You might want to try the built in helper http://api.rubyonrails.org/classes/ActionView/Helpers/NumberHelper.html#method-i-number_to_phone I believe it outputs in US only

Use sub:
phone = 3012944070
phone.to_s.sub(/(\d{3})(\d{3})(\d{4})/, '(\1) \2-\3')
# => "(301) 294-4070"

Related

Bin Function with Squared Brackets [duplicate]

This question already has answers here:
Understanding slicing
(38 answers)
Closed 1 year ago.
def add_binary(a,b):
return bin(a+b)[2:]
Why is "[2:]" used here?
convert number into it's binary and remove first two characters 0b.
Example:
Original number: 10
Binary string: 0b1010
after that it will be 1010 only.

how i can do limit count of inputs before "#" for email in ruby [duplicate]

This question already has answers here:
Validation issue in email for minimum length in javascript
(2 answers)
regex for email validation (minimum characters) [closed]
(2 answers)
Email validation- characters length before # and before dot
(3 answers)
Limit length of characters in a regular expression
(6 answers)
Closed 2 years ago.
For example {limit [5-50] for here}#email.com
email#hotmail.com -> incorrect, because count of characters input smaller then 5
tryemail#hotmail.com -> correct
Can you explain the solution ?
Maybe as simple as this:
local_part, domain = email.split('#')
local_part.length < 5
Though keep in mind many people do have short addresses, so don't get too strict here.
You could also do this with a regular expression:
email.match?(/\A[^#]{5,50}#[^#]+\z/)
Where that's an extremely lax parser.

How do you confirm a string only contains numbers in Swift? [duplicate]

This question already has answers here:
Finding out whether a string is numeric or not
(18 answers)
Closed 7 years ago.
How can I check, if searchView contains just numbers?
I found this:
if newText.isMatchedByRegex("^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$") { ... }
but it checks if text contains any number. How can I do, that if all text contains just numbers in Swift?
Here is the solution you can get all digits from String.
Swift 3.0 :
let testString = "asdfsdsds12345gdssdsasdf"
let phone = testString.components(separatedBy: CharacterSet.decimalDigits.inverted).joined(separator: "")
print(phone)
you can use "^[0-9]+$" instade "^(?:|0|[1-9]\\d*)(?:\\.\\d*)?$"
This will accept one or more digits, if you want to accept only one digit then remove +

Alternatives to #""? [duplicate]

This question already has answers here:
How to escape double quotes in string?
(3 answers)
Closed 10 years ago.
I have a string that has multiple " in it, which is written inside of #"" and of course, xcode sees this as me ending the #". Is there any alternatives I can use for #"" that would do the same thing?
It's done with escape chars. #"My name is \"Someone\". Blabla.";

What does "temps.each(&:valid?)" mean in Ruby? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What does map(&:name) mean in Ruby?
What does the &:valid? found in the each mean?
I've seen .each do |r| or whatever, but not sure how this one works?
The & is called the to_proc operator. It expands the symbol (:valid?) into a Proc.
So your example is equivalent to:
temps.each { |t| t.valid? }
&:symbol is a shorthand for symbol to proc.
Here's a good blog post on it.
http://blog.hasmanythrough.com/2006/3/7/symbol-to-proc-shorthand

Resources