开发者

How can I check whether a parameter isa Symbol?

开发者 https://www.devze.com 2022-12-08 01:07 出处:网络
The question is in the title. My parameter can be either a string or a symbol and depending upon which it is I want to pe开发者_开发知识库rform different actions.Is there a way to check for this in R

The question is in the title.

My parameter can be either a string or a symbol and depending upon which it is I want to pe开发者_开发知识库rform different actions. Is there a way to check for this in Ruby?


def foo(arg)
  if arg.is_a?(Symbol)
    do_symbol_stuff
  else
    do_string_stuff
  end
end


Another solution

def foo(arg)
  case arg
    when Symbol
      do symbol stuff
    when String
      do string stuff
    else
      do error stuff
  end
end
0

精彩评论

暂无评论...
验证码 换一张
取 消