make sure your method returns either nil or false :
def this_may_fail some_logic rescue nil end
you can use the if modifier to check the return value of your method and assign a value only if it is not nil or false :
post['Caption'] = this_may_fail if this_may_fail
or you can cache the return value of this_may_fail in a local variable if you don't like calling the method twice for the if condition and assignment.
the_value = this_may_fail post['Caption'] = the_value if the_value
also notices that the rescue modifier only catches StandardError and its subclasses.
rubiii
source share