Ruby.
We can write code in four different ways:
Usage if:
def bar(foo)
foo.to_i if foo
end
Usage unless:
def bar(foo)
foo.to_i unless foo.nil?
end
Using the method trywhen using Rails:
def bar(foo)
foo.try(:to_i)
end
Using the operator &.(Ruby's secure navigation operator) when using Ruby 2.3.x:
def bar(foo)
foo&.to_i
end
For a more idiomatic way to write Ruby code , I read a great post about it.
source
share