Assing name rubby no work

Hello I am developing this:

campo3 = $evm.root[‘dialog_campo1’]

if campo3 = 2
result = “vlan ok”
end

unless campo3.nil?
param_campo2 = $evm.object
param_campo2[“data_type”] = “string”
param_campo2[“required”] = “true”
param_campo2[“value”] = result

I have 2 dialog, dialog campo1 copy result to dialog campo 2 with this method.

What I want to do is that when I put the number 2 in field1, I will automatically update field2 with the name of the variable result.

Hi @zetas1982,

I think there is a bug with the code you’ve posted, which might be part of your issue?

if campo3 = 2
result = “vlan ok”
end

This code is assigning the value 2 to the campo3 variable and not doing a comparison. This assignment will always evaluate to true, and thus the result will always be “vlan ok”. If you’re trying to do comparison, you want the == operator instead (i.e. if campo3 == 2)

On a related note, the variable campo3 can’t be nil, if the field dialog_campo1 exists in the dialog, it will have a default value (e.g. the empty string “”). You could use campo3.blank? which checks for nil and a couple of other cases (most notably empty strings/collections, string with only whitespaces and the boolean false)
However since you are dealing with integers: zero is not considered blank

I would write the code like this:

value = $evm.object['dialog_campo1']

if value.respond_to?(:to_i) && value.to_i == 2
    result = "vlan ok"
else
    result = value.to_s
end

$evm.object['data_type'] = "string"
$evm.object['required'] = true
$evm.object['value'] = result

If you don’t like the if-statement, this has the same effect. Depends on how explicit you want to be with your type checking and how comfortably you are with ruby/rails

# This if statement has the same effect as the if above
if value.try(:to_i) == 2
  result = "vlan ok"
else
  result = value.to_s
end