Reputation: 3365
What is a good way of writing something like this in one line. The goal is to log the message at info level if the value is nil else in debug mode, along with proper message like the one shown. Seems simple, but writing 5 lines of code for something small like this keeps bothering me. I can wrap this in method, but would like to avoid if I can. Thanks.
if value.nil?
Rails.logger.info "value: nil"
else
Rails.logger.debug "value: #{value.inspect}"
end
Upvotes: 3
Views: 223
Reputation: 369438
If you want it in one line, just remove the line breaks:
if value.nil? then Rails.logger.info "value: nil" else Rails.logger.debug "value: #{value.inspect}" end
But why, oh why, would anyone want to do this?
Upvotes: 0
Reputation: 1806
value.nil? ? Rails.logger.info "value: nil" : Rails.logger.debug "value: #{value.inspect}"
Upvotes: 0
Reputation: 4109
value.nil? ? (Rails.logger.info "value: nil") : (Rails.logger.debug "value: #{value.inspect}")
Another way you can try (this is not the same, but similar):
Rails.logger.send (value.nil? ? :info : :debug), "value: #{value.inspect}"
But the best way I think is to write you own logger, that automatically decides which variable in which flow is written:
NullLogger.log value
Upvotes: 8
Reputation: 2248
value ? Rails.logger.debug("value: #{value.inspect}") : Rails.logger.info("value: nil")
Upvotes: 0