This question already has answers here:
What does the !~ method do with String in Ruby
(2 answers)
Closed 8 years ago.
When declaring syntax such as:
a !~ b
where a,b are variables, what does it mean?
It is negation of =~, a regex match.
"a" !~ /b/
# => true
It is useful when you want to check whether a string does not match a certain pattern. For example, if you want to check if string s includes only numbers, then you can do:
s !~ /\D/
Related
This question already has answers here:
What is the !=~ comparison operator in ruby?
(2 answers)
Closed 2 years ago.
I am modifying an existing ruby code. It has the following lines of code. Can somebody tell me what is going on.
if string ==~ /^ABC/
do-something
elsif string == "some string"
do-something
else
do-something
end
What is the if condition doing here. I googled for ==~ operator and found nothing.
I just found explanation for =~, which means matching strings with regular expressions.
So, if the above if condition has single = , it means check if string starts with ABC. But that is not happening when i run the code. Even though string starts with ABC, it doesn't go into if.
I am not sure if it is a mistake or intentional usage of ==~
The unary ~ operator has higher precedence than == or =~ so this:
string ==~ /^ABC/
is just a confusing way of writing:
string == (~/^ABC/)
But what does Regexp#~ do? The fine manual says:
~ rxp → integer or nil
Match—Matches rxp against the contents of $_. Equivalent to rxp =~ $_.
and $_ is "The last input line of string by gets or readline." That gives us:
string == (/^ABC/ =~ $_)
and that doesn't make any sense at all because the right hand side will be a number or nil and the left hand side is, presumably, a string. The condition will only be true if string.nil? and the regex match fails but there are better ways to doing that.
I think you have two problems:
==~ is a typo that should probably be =~.
Your test suite has holes, possibly one hole that the entire code base fits in.
See also What is the !=~ comparison operator in ruby? for a similar question.
This question already has answers here:
Where is Ruby's string literal juxtaposition feature officially documented?
(4 answers)
Ruby backslash to continue string on a new line?
(3 answers)
Closed 8 years ago.
If I have this code:
a = "hi" "pie"
puts a
It will print out hipie. Does Ruby automatically combine these?
Yes. From Literals: String
Adjacent string literals are automatically concatenated by the interpreter:
"con" "cat" "en" "at" "ion"
#=> "concatenation"
"This string contains " "no newlines."
#=> "This string contains no newlines."
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the colon operator in Ruby?
While learning Ruby I've come across the ":" operator on occasion. Usually I see it in the form of
:symbol => value
what does it mean?
It just indicates a that it is a symbol instead of a string. In ruby, it is common to use symbols instead of strings.
{:foo => value}
{'foo' => value}
It's basically a short-hand way of expressing a string. It can not contain spaces as you can imagine so symbols usually use underscores.
Try this on your own:
foo = :bar
foo.to_s # means to string
baz = 'goo'
baz.to_sym # means to symbol
This question already has answers here:
What is the "=~" operator in Ruby?
(7 answers)
Closed 8 years ago.
In ruby, I read some of the operators, but I couldn't find =~. What is =~ for, or what does it mean? The program that I saw has
regexs = (/\d+/)
a = somestring
if a =~ regexs
I think it was comparing if somestring equal to digits but, is there any other usage, and what is the proper definition of the =~ operator?
The =~ operator matches the regular expression against a string, and it returns either the offset of the match from the string if it is found, otherwise nil.
/mi/ =~ "hi mike" # => 3
"hi mike" =~ /mi/ # => 3
"mike" =~ /ruby/ # => nil
You can place the string/regex on either side of the operator as you can see above.
This operator matches strings against regular expressions.
s = 'how now brown cow'
s =~ /cow/ # => 14
s =~ /now/ # => 4
s =~ /cat/ # => nil
If the String matches the expression, the operator returns the offset, and if it doesn't, it returns nil. It's slightly more complicated than that: see documentation here; it's a method in the String class.
=~ is an operator for matching regular expressions, that will return the index of the start of the match (or nil if there is no match).
See here for the documentation.
This question already has answers here:
Validate that string contains only allowed characters in Ruby
(4 answers)
Closed 2 years ago.
Given a string of a mobile phone number, I need to make sure that the given string only contains digits 0-9, (,),+,-,x, and space. How can I do it in Ruby?
Use:
/^[-0-9()+x ]+$/
E.g.:
re = /^[-0-9()+x ]+$/
match = re.match("555-555-5555")
if (/^[-\d()\+x ]+$/.match(variable))
puts "MATCH"
else
puts "Does not MATCH"
end
Use String#count:
"+1 (800) 123-4567".count("^0-9+x()\\- ").zero? # => true
"x invalid string x".count("^0-9+x()\\- ").zero? # => false