Answer the question
In order to leave comments, you need to log in
What is the easiest way to set a variable in ruby if it is empty or not equal to another variable?
Hello. Let's say we have an object man = men.find_or_create_by!(id: 1)
It has an age parameter. And we will have it either nil if the object is new, or equal to some value. And we have one more variable, let's say age2. What is the prettiest way to set man.age to age2 if they are different?
While I decided it like this:
But something tells me that it is possible to make it more beautiful. man.age.replace(age2) if age != age2
Answer the question
In order to leave comments, you need to log in
The question is contrived and abstract. Do exactly the same, but through native setters, and not through replay.
But when the example becomes not too abstract, even if it’s rubbish and sugar language, then write better anyway:
if man.age != woman.age
man.age = woman.age # WTF qunatum leap?
end
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question