0

I have below hash

instance_options[:param_page]
instance_options[:param_page][:comment_page]
instance_options[:param_page][:comment_per]

I need comment_page, comment_per value I did refactoring code But I not sure what is best hash null check let me know best null check if you know anothers

befor

comment_page = 1
comment_per = 0
param_page = instance_options[:param_page]
if param_page
  comment_page = param_page[:comment_page].presence || comment_page
  comment_per = param_page[:comment_per].presence || comment_per
end

after

comment_page = instance_options[:param_page][:comment_page].present? ? instance_options[:param_page][:comment_page] : 1
comment_per = instance_options[:param_page][:comment_per].present? ? instance_options[:param_page][:comment_per] : 10
Changwoo Rhee
  • 179
  • 4
  • 11

1 Answers1

1

You go a step further:

comment_page = instance_options.dig(:param_page, :comment_page).presence || 1
comment_per  = instance_options.dig(:param_page, :comment_per).presence || 0

That will go digging into instance_options twice but the cost will be trivial. If you need to worry about types (say the :comment_page value could be a string), then you could add #to_i calls:

comment_page = (instance_options.dig(:param_page, :comment_page).presence || 1).to_i
comment_per  = (instance_options.dig(:param_page, :comment_per).presence || 0).to_i

Or use reverse_merge (since that's for setting defaults):

param_page = instance_options[:param_page] || {}
param_page.reverse_merge(comment_page: 1, comment_per: 0)
# Then use param_page[:comment_page] and param_page[:comment_per] instead of two locals
mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • 1
    `reverse_merge` is aliased as `with_defaults` so this exactly the sort of thing it is for. I use the `presence` technique (and `#to_a`, `#to_h`, `#to_i`, ...) all the time to "hide" `nil` checks. – mu is too short Jan 30 '20 at 22:34
  • I changed code to ```post_page = instance_options.dig(:pagaination_param, :post_page).presence.to_i || 1``` and added to_i because post_page type not be int. i got such a type error – Changwoo Rhee Jan 30 '20 at 23:05
  • yes. i did read https://apidock.com/rails/Object/presence but value of ```nil.to_i``` is 0. it's can't be null check. then should i change code your comment of ```post_page = (instance_options.dig(:pagaination_param, :post_page).presence || 1).to_i``` ? ```instance_options.dig(:pagaination_param, :post_page).to_i.presence || 1``` is correct? because ```to_i``` chage nil to 0 – Changwoo Rhee Feb 06 '20 at 00:31
  • i think 0 is not nil and i did run console ```$ rails c``` ```0.presence == nil => false ``` i got false. i think ```...to_i.presence``` is incorrect – Changwoo Rhee Feb 06 '20 at 07:44
  • 1
    @ChangwooRhee Right, sorry, getting myself confused. I think you'd want `comment_page = (instance_options.dig(:param_page, :comment_page).presence || 1).to_i` if you want to always get an integer back. – mu is too short Feb 06 '20 at 19:25