This works but I am trying to figure out a better way to write it. I want to replace this if condition. I have multiple cases like this
if text == "a" || text == "ac" || text == "acc" || text == "acco" || text == "accou"
|| text == "accoun" || text == "account"
{
flagAccount = false
} else {
flagAccount = true
}
I thought of using CharacterSet but cant seem to figure it out
The condition is equivalent to
"account".hasPrefix(text) && text != ""
Note hat text != "" is needed because "account".hasPrefix("") is true.
As Rob suggested, you might write this in an if statement like so, using !text.isEmpty might be nit more readable:
if !text.isEmpty, "account".hasPrefix(text) {
}
Related
row = {"joining_date"=>"18/07/2015", "name"=>" Joe Doe ", "company"=>" Google", "location"=>" New York ", "role"=>"developer", "email"=>"joe#doe.com", "mobile"=>"11-(640)123-45674", "address"=>"4 XYZ Road", "validity"=>"true"}
row is invalid only if any one of the fields(joining_date, name, company, location, email, address) is nil or not present.
def is_valid?
valid = true
if row[:name] == nil || row[:joining_date] == nil || row[:address] == nil || row[:email] == nil || row[:company] == nil || row[:location] == nil
valid = false
end
valid
end
Is there any way that I can simplify and refactor the above method in rails to find it more efficient using regex?
Probably, but I wouldn't use a regex as it's in a hash. As you're using rails you can use present? or blank?.
row.values.any?(&:blank?)
Would return true if any are blank
for your case
def is valid?
row.values.all?(&:present?)
end
I have a uitextfield and i have got a search option using this uitextfield.When i enter the text it works well but when i clear the text i get "object returned empty description check" when i print it using po command in console. For checking this i have used the following code:
if(_nameTextField.text !=nil || _nameTextField.text.length !=0 || ![_nameTextField.text isEqual:#""]|| _nameTextField.text != NULL || ![_nameTextField.text isEqualToString:#""]){
}
But still it enters into the loop
use && instead of ||
if(_nameTextField.text !=nil && _nameTextField.text.length !=0 && ![_nameTextField.text isEqual:#""]&& _nameTextField.text != NULL && ![_nameTextField.text isEqualToString:#""]){
}
Please use this..it help you:
if (_nameTextField.text && _nameTextField.text.length!=0)
{
}
Use like this, it will work
if(_nameTextField.text !=nil && _nameTextField.text.length > 0 ){
//...
}
This question already has an answer here:
If condition failing with expression too complex
(1 answer)
Closed 7 years ago.
Now, I've seen this swift error posted all over stackoverflow. However, the way they handle it won't seem to work in my situation.
if (
fieldType == "checkbox" ||
fieldType == "time" ||
fieldType == "birthdate" ||
fieldType == "datetime" ||
fieldType == "phone" ||
fieldType == "payment" ||
fieldType == "stripe" ||
fieldType == "paypal" ||
fieldType == "paypalpro" ||
fieldType == "address" ||
fieldType == "2co")
{
Not sure how I can fix it. There must be some way to lower the complexity of it.
What I would do is just start with an array of literals:
let fieldType = "Howdy"
let possibilities = ["checkbox","time"] // ... add the others too...
if find(possibilities, fieldType) != nil { // or: if contains(possibilities,fieldType)
}
I am developing MVC app and I am using the LINQ in controller.
I am trying to get one rechord with below query, but its giving an error...
Approval oAP = new Approval();
oAP = db.Approvals.Where(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount));
Is there any wrong syntax ?
Got the answer
oAP = db.Approvals.Where(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount)).FirstOrDefault();
Change this
e.ApprovedBy.Id = loggedEmployee.Id
For
e.ApprovedBy.Id == loggedEmployee.Id
You're comparing not assigning values.
Also you may add this
oAP = db.Approvals.Where(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount)).FirstOrDefault();
Because i'm assuming that you want to return only one
Some remarks:
You should be able to drop the Where:
oAP = db.Approvals.FirstOrDefault(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount));
Personally, I try to avoid the First and FirstOrDefault functions, because if you know there is only one record and if you want to enforce this, you can use SingleOrDefault:
oAP = db.Approvals.SingleOrDefault(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount));
If you know there will always be (more than) one record, you can drop the 'OrDefault' part and use First() or Single().
I usually do
value = input || "default"
so if input = nil
value = "default"
But how can I do this so instead of nil It also counts an empty string '' as nil
I want so that if I do
input = ''
value = input || "default"
=> "default"
Is there a simple elegant way to do this without if?
Rails adds presence method to all object that does exactly what you want
input = ''
value = input.presence || "default"
=> "default"
input = 'value'
value = input.presence || "default"
=> "value"
input = nil
value = input.presence || "default"
=> "default"
I usually do in this way:
value = input.blank? ? "default" : input
In response to the case that input might not be defined, you may guard it by:
value = input || (input.blank? ? "default" : input)
# I just tried that the parentheses are required, or else its return is incorrect
For pure ruby (not depending on Rails), you may use empty? like this:
value = input || (input.empty? ? "default" : input)
or like this (thanks #substantial for providing this):
value = (input ||= "").empty? ? "default" : input
Maybe irrelavant but I would use highline like this:
require "highline/import"
input = ask('Input: ') { |q| q.default = "default" }
It works without Rails. Really neat solution.