What is a elegant way in Ruby to tell if a variable is a Hash or an Array?

前端 未结 9 2086
清酒与你
清酒与你 2021-01-31 07:02

To check what @some_var is, I am doing a

if @some_var.class.to_s == \'Hash\' 

I am sure there is a more elegant way to check if

9条回答
  •  失恋的感觉
    2021-01-31 07:15

    Hash === @some_var #=> return Boolean
    

    this can also be used with case statement

    case @some_var
    when Hash
       ...
    when Array
       ...
    end
    

提交回复
热议问题