M
M
Maxim Valerievich2015-06-02 12:05:14
ruby
Maxim Valerievich, 2015-06-02 12:05:14

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

2 answer(s)
T
thepry, 2015-06-02
@thepry

man.age = age2doesn't fit?

V
Viktor Vsk, 2015-06-02
@viktorvsk

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 question

Ask a Question

731 491 924 answers to any question