How can I check if a parameter is a Symbol?

The question is in the title.

My parameter can be either a string or a character and depending on what I want to do different things for. Is there a way to test this in Ruby?

+2


source to share


2 answers


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

      



+5


source


Another solution



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

      

+2


source







All Articles