Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby idiom for do_two_simple_things if something_is_true

Tags:

ruby

For instance, this takes 4 lines which is too much space for such a simple operation:

if something_is_true
  puts 'error'
  return
end

this one is a one-liner but looks awkward.

if something_is_true; puts 'error'; return; end

Can we do something like

# it would be great if this would work because it is short and readable
puts 'error' and return if something_is_true
like image 391
powerboy Avatar asked Oct 15 '25 16:10

powerboy


1 Answers

I'm not sure why you think space is at such a premium that your original code is "too much." Give your code room to breathe, and let it take the space it needs. Getting too tricky in order to "save space" is a false economy. The most important thing is that your code be readable and understandable. Your original code looks great to me.

like image 170
Ned Batchelder Avatar answered Oct 18 '25 07:10

Ned Batchelder



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!