This question already has answers here:
Check whether a string contains one of multiple substrings
(8 answers)
Closed 6 years ago.
i've written a few lines of code like this :
if( (user_input.include?('string_a') ||
(user_input.include? ('string_b')) ||
(user_input.include?('string_c')) )
&&
user_input.include?('string_d_keyword'))
....
end # if
is there any function which can simplify the "multiple or string match" by taking multiple arguments and look like this ?
if( (user_input.multi_include_or?('string_a','string_b','string_c'))
&& (user_input.include?('string_d_keyword')))
.....
end # if
i hope to do these all in a single line and so i've leave out the option of "case when".
Thanks~
You can do a regex match using | (or):
if user_input.match? /string_a|string_b|string_c|string_d_keyword/
…
end
If your strings are in an array you can use Regexp.union to convert them to the corresponding regex:
if user_input.match? Regexp.union(strings)
…
end
Use an array and any?
> user_input = "string_a"
=> "string_a"
> ["asd","string_a"].any? {|a| user_input.include? a}
=> true
Related
This question already has answers here:
Don't understand Ruby ljust/rjust/center methods
(2 answers)
Closed 5 years ago.
I'm using Ruby 2.4. HOw do I add an arbitrary amount of spaces to the end of my string? I thought it was ljust but
2.4.0 :003 > line = "abcdef"
=> "abcdef"
2.4.0 :004 > line = line.ljust(4, " ")
=> "abcdef"
Notice my string is unchanged. What am I doing wrong?
The integer to ljust() must be larger than the length of the string, or nothing will be appended. Since line is six chars, I believe you want:
line = "abcdef"
line = line.ljust(10, " ")
That'll add four spaces after the six characters already present in the string.
You could likely also do something along the lines of:
line = line.ljust(line.length + 4, " ")
You can add a multiple of a spaces:
line = "abcdef"
line + ' '*5
#=> "abcdef "
line
#=> "abcdef"
Or use concat which modifies the string.
line.concat(' '*5)
#=> "abcdef "
line
#=> "abcdef "
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/
This question already has answers here:
How to compare strings ignoring the case
(5 answers)
Closed 7 years ago.
I want to test 2 strings for equality in Ruby in a case insensitive manner.
In languages, such as Fantom, you simply write:
string1.equalsIgnoreCase(string2)
What's the idiomatic way to do this in Ruby?
You can use casecmp
"Test".casecmp("teST")
=> 0
"Test".casecmp("teST2")
=> -1
So to test for equality, you can do:
if str.casecmp(str2).zero?
# strings are equal
end
Though there is casecmp:
0 == s1.casecmp(s2) # strings equal
I personally prefer
s1.downcase == s2.downcase
You can convert the strings to lowercase and then compare
a.downcase == b.downcase
Or, if you prefer, to uppercase
a.upcase == b.upcase
You can use String#match method :
s = "Test"
s.match(/teST/i) # => #<MatchData "Test">
s.match(/teST2/i) # => nil
Remember in Ruby all objects are has the truth value, except nil and false. So you can use this trick also to perform conditional testing.
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