Is there a way to match the following string so I get user name and 50?
hey "user name":/users/50
I may also have more than one instance of this in a string.
can you try the following
string = 'hey "user name":/users/50'
matches = string.scan /"(?<name>[A-Za-z ]+)":\/users\/(?<user_id>\d+)/
matches will be an array containing arrays with 2 elements where the first element is the name and the 2nd element is the user_id
>> matches # [['user name', 50]]
Related
json[:errors] = ["Username can't be blank", "Email can't be blank"]
The error, in en.yml, itself is provided as:
username: "can't be blank",
email: "can't be blank"
and the test:
expect(json[:errors]).to include t('activerecord.errors.messages.email')
Which fails because it's looking at the string "Email can't be blank", and "can't be blank" doesn't match it.
My question is what is the best (and by that I mean best practice) way to test
that the substring is included in a string contained inside the array json[:errors]
RSpec offers a range of matchers. In this case you'll need to use the include matcher (docs) to check each element of the array. And, you'll need to use the match regex matcher (docs) to match the substring:
expect(json[:errors]).to include(match(/can't be blank/))
For readability, the match regex matcher is aliased as a_string_matching, like this:
expect(json[:errors]).to include(a_string_matching(/can't be blank/))
UPDATE:
I just noticed that the OP's question include an array with multiple matching elements. The include matcher checks to see if ANY elements of the array match the criteria. If you to check to see if ALL elements of the array match the criteria, you can use the all matcher (docs).
expect(json[:errors]).to all(match(/can't be blank/))
I'm having a Rails app running with PostgreSQL. There is a table called Programs as the following:
Program(id: integer, name: string, properties: jsonb)
Let say we have a program called "Football Club" where properties are as the following:
{
"properties" =>
{
"What's your gender?" => "I'm male.",
"Is your health good?" => "Yes"
}
}
My query is like this:
# this field and value are OK with the query
field = Is your health good?
value = Yes
Program.where("properties ->> '#{field}') = '#{value}'")
# this field and value causes error with the query since they contain quote (')
field = What's your gender?
value = I'm male.
Program.where("properties ->> '#{field}') = '#{value}'")
I know that the issue causes by how I interpolate string. Yet, I could not find a solution.
Really appreciate for your help.
I have an array of custom objects in it. Those objects have a parameter called name which is a concatenation of 2 strings having a delimiter in between. Eg: name could be Some#Data where 'Some' is first string and 'Data' is another and # is a delimiter.
My intention is update the name parameter for all the objects inside the array such that the param would only have 'Data' (i.e. remove 'Some#') and store the objects inside another array after updating. Below is the code:
final_array = array1.select do |object|
object.name = object.name.match(/#(.*?)$/)
end
When I print object.name.match(/#(.*?)$/) this gives me output as:
#<MatchData "#Data" 1:"Data">
Out of this output, how do I get "Data" from this MatchData. I tried object.name.match(/#(.*?)$/)[1] but it didn't work. Or do I need to change my regex?
I would use #each and #gsub methods:
array.each do |object|
object.name = object.name.gsub(/^.+#/, '')
end
I have the following in my url. I need to extract both 4 and 2 separately for the purpose of searching. These two integer, one is category id and other is sub category id
params[:id].scan(/\d+$/).first
using the above scan i can get 4 but how can i get more than one integers
my-url-for-test-4-2
I have created a helper method
def string_to_int(param,key)
param.scan(/\d+/).map(&:to_i).key
end
And i tried it from my controller like this
id = string_to_int(params[:id],'first')
But getting error as
undefined method `key' for [4, 2]:Array
Why it is not acception.
Answer lies in your question
params[:id].scan(/\d/) will result in array.
params[:id].scan(/\d+/).map(&:to_i)
If you are passing first or last as key :
def string_to_int(param,key)
param[:id].scan(/\d+/).map(&:to_i).send(key)
end
You can match against the last two numerical parts (separated by hyphens) in your :id parameter:
string = params[:id].to_s
ids = string.scan(/-(\d+)-(\d+)$/)[0] # .try(:map, &:to_i)
Update:
Here's a not-too-edge case that would be handled well:
a = "random1-2string-3-4-5"
a.scan(/-(\d+)-(\d+)$/) # => [["4", "5"]]
a.scan(/-(\d+)-(\d+)$/)[0] # => ["4", "5"]
a.scan(/-(\d+)-(\d+)$/)[0].try(:map, &:to_i) # => [4, 5]
Ruby => How to match a string that contains #and a number example #56
for example i have a string "my roll number is #256767"
i should match whether the string has # symbol and a number beside it
Use Ruby's match
!"my roll number is #256767".match(/#\d+/).nil?
Use something like:
m = s.match(/^.*(#\d+).*$/)
unless m.nil?
puts m[1]
end