Explaining the error you saw:
Somehow, when trying to split an element in an array, split is no
longer working.
I get this error:
private method `split' called for #<Array:0xa7ad1ba4> (NoMethodError)
I can't show the code which generates the array, but I can reproduce
the error with something simple like this:
puts ["a\nb", "c"].split("\n")
Your problem here is that you call Kernel.split which is private
because it's intended to be used like this:
irb(main):009:0> $_="foo\nbar"; split /\n/
=> ["foo", "bar"]
You can find that out like this:
irb(main):001:0> .method :split
=> #<Method: Array(Kernel)#split>
This is typically used in command line one liners
ruby -n -e 'puts split(/foo/)' some_file
However, imagine if you would that code like this could generate the
same private method error.
array = ["a\nb", "c"]
puts array[0].split("\n")
(where 'array[0]' is an element in a slightly complex array)
No, because now you are calling String#split which is actually public.
irb(main):011:0> "foo\nbar".split /\n/
=> ["foo", "bar"]
Here you can see that it's a different method:
irb(main):002:0> "".method :split
=> #<Method: String#split>
Why can't I use split with an element in my array? What's going on here?
I can do { puts array[0].inspect } and everything is normal..
My goal is to convert an array which has strings of varying sizes (one
or more lines) into an array where each element has just one line of
text. Maybe there's a better way to do this and I can avoid this
wierdness?
Solutions have been suggested already. Here's another one that avoids
the use of flatten because that is less efficient:
irb(main):013:0> ["foo\nbar", "buz", "bat\ndos"].inject(){|ar,s|
ar.concat(s.split(/\n/))}
=> ["foo", "bar", "buz", "bat", "dos"]
Kind regards
robert
···
2006/6/4, Sy Ali <sy1234@gmail.com>:
--
Have a look: Robert K. | Flickr