Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's a more idiomatic Ruby way of writing this?

  if params[:parent_type] == "Order"
    parent_id = nil
  else
    parent_id = params[:parent_id]
  end

Would a Ruby person laugh at me for writing it this way? It doesn't seem particularly concise like some Ruby code I've seen.

like image 796
nova Avatar asked Nov 29 '22 05:11

nova


1 Answers

That looks perfectly reasonable to me. You could move the assignment in front of the if ( parent_id = if params...) or use the ternary, but I don't think the result would look better.

If parent_id is nil or undefined before that line you can simply write:

parent_id = params[:parent_id] unless params[:parent_type] == "Order"
like image 123
sepp2k Avatar answered Dec 22 '22 22:12

sepp2k