I like the readability of:
if !params[:foo].blank?
stuff
end
Or just:
stuff if !params[:foo].blank?
But I didn't know if I was giving myself any performance gain in writing it this way. Finally I took the time to Benchmark it using the following:
iterations = 1_000_000
Benchmark.bm do |bm|
# using if &&
bm.report do
iterations.times do
if params[:event_type] && params[:event_type] != ''
with['event_type'] = params[:event_type].to_i
end
end
end
# using !blank?
bm.report do
iterations.times do
if !params[:event_type].blank?
with['event_type'] = params[:event_type].to_i
end
end
end
# using unless blank?
bm.report do
iterations.times do
unless params[:event_type].blank?
with['event_type'] = params[:event_type].to_i
end
end
end
end
And the results are:
user system total real
3.190000 0.010000 3.200000 ( 3.215461)
3.460000 0.010000 3.470000 ( 3.481160)
3.490000 0.010000 3.500000 ( 3.509005)
The least ruby-like method of writing this turns out to be the fastest...