yasuoza diary

web and life

How do you indent ruby's if..else assignment?

There are several ways to write if...else variable assignment.

1
2
3
4
5
@products = if params[:category]
              Category.find(params[:product]).products
            else
              Products.all
            end
1
2
3
4
5
@products = if params[:category]
  Category.find(params[:product]).products
else
  Products.all
end
1
2
3
4
5
@products = if params[:category]
    Category.find(params[:product]).products
  else
    Products.all
  end
1
2
3
4
5
6
@products =
  if params[:category]
    Category.find(params[:product]).products
  else
    Products.all
  end

Which one do you prefer?

I prefer last one. Because this can keep horizontal length much smaller and vim-ruby’s auto indent also supports this syntax.

FYI: http://stackoverflow.com/questions/8745299/recommended-indentation-style-for-ruby-if-blocks-that-assign-a-value-to-a-vari http://stackoverflow.com/questions/2925028/how-do-you-assign-a-variable-with-the-result-of-a-if-else-block

Comments