def my_method(parameter) if <what should be here?> puts "parameter is a string" elsif <and here?> puts "parameter is a symbol" end end 5 Answers
The simplest form would be:
def my_method(parameter) puts "parameter is a #{parameter.class}" end But if you actually want to do some processing based on type do this:
def my_method(parameter) puts "parameter is a #{parameter.class}" case parameter when Symbol # process Symbol logic when String # process String logic else # some other class logic end end 4 Comments
Peter Brown
Shouldn't that be
case parameter.class?Thiago Silveira
@Beerlington I just tested, and it works fine using only parameter.
Antal Spector-Zabusky
@Beerlington: No: in a
case x; when y ....; end, Ruby runs y === x to determine if it should enter the when block. === is just defined to do something "useful"; for instance, for classes, it's instance_of?; for ranges, it's include?, etc.