P
P
Philipp2015-02-24 07:10:04
Ruby on Rails
Philipp, 2015-02-24 07:10:04

How to replace the ternary operator?

There is a code like this:

margin = @supply.supplier.margin ? @supply.supplier.margin : 0

If there is some general approach, for example, to check for the presence of a supplier, margins and return 0 if they are absent, tell me. I'm blunting in general.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Viktor Vsk, 2015-02-24
@zoonman

@supply.supplier.margin || 0
It will not give you what you want when the margin is equal ""(what happens if you change it to an empty value through the form)
For this, they made a method in Rails.
Which is equivalent to:
present? checks for empty values ​​better than implicit type casting

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question