ruby - How to make a method to split two strings in an array? -
i'm trying return 2 strings in array individual words:
list = ['hello name ryan', 'hole me llamo'] def splitter(inp) inp.each.split(' ') end print splitter(list)
this returns:
ruby splitter.rb splitter.rb:4:in `splitter': undefined method `strsplit' # <enumerator: ["hello name ryan", "hole me llamo"]:each> (nomethoderror) splitter.rb:7:in `<main>'
it works if don't use .each
, use inp(0)
or inp(1)
1 string returns.
how can both strings returned?
here 1 should :
def splitter(inp) inp.flat_map(&:split) end splitter list # => ["hello", "my", "name", "is", "ryan", "hole", "me", "llamo"]
in code inp.each
method call array#each
, without block gives enumerator
. , string#spilt
exist, there not method enumerator#split
, that's why nomethod error blows up.
and if want array of words each individual strings,
def splitter(inp) inp.map(&:split) end splitter list # => [["hello", "my", "name", "is", "ryan"], ["hole", "me", "llamo"]]
Comments
Post a Comment