Chris Xue Chris Xue - 6 months ago 13
Ruby Question

Ruby idiom for "foo.nil? ? nil : foo.to_i"?

def bar(foo)
foo.nil? ? nil : foo.to_i
end


Any concise Ruby idiom for "foo.nil? ? nil : foo.to_i" ?

Answer

Or a little bit shorter (if you dont expect foo to be false)

def bar(foo)
  foo.to_i if foo
end