RockPaperScissors

LizardSpock

Ruby Duck Typing Strings and Arrays

| Comments

How to get method which handles a single string input or an array of strings?

Splat *

* turns single strings into Arrays, leaves arrays alone.

a = "hello"
a = *a
puts a.inspect
=>  ["hello"]

a = ["hello", "world"]
a = *a
puts a.inspect
=> ["hello", "world"]

Example Usage

def unknown_input( input_data )
  input_data = *input_data
  input_data.each do |input_item|
      puts input_item
  end
end

Comments