Damian
Damian

Reputation: 1553

How can I check whether a parameter isa Symbol?

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 perform different actions. Is there a way to check for this in Ruby?

Upvotes: 2

Views: 2918

Answers (2)

Scott
Scott

Reputation: 7274

Another solution

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

Upvotes: 2

sepp2k
sepp2k

Reputation: 370357

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

Upvotes: 5

Related Questions