From: Mark S Bilk Date: 2009-04-06T23:54:16+09:00 Subject: Re: String.split crashes my program On Apr 6, 7:21 am, Tomas Na wrote: > I am trying to split a string by spaces and then loop through each > entry, adding it to an array if it is more then 4 characters long and > not already in the array. > > I haven't got very far with it, but I have come up against this problem > where split just crashes my program with no errors. > > def GetUniqueTextValues(sText) >   aTags = Array.new > >   puts sText >   aTags = sText.split >   puts sText > >   return aTags > end > > What happens when I run this is the text is output for the first puts, > but not for the second. If I comment out the split then the text is > output twice, once for each puts. I read in the ruby docs that split > with no parameters will split by space, but I also tried split(' ') and > split(/ /) with the same crashing effect. I use split another place in > my program like this: > > saHeadParts = line.split(/: /) > > and it works fine, so I don't understand why it wont work this time. Can > anyone help me fix this? > -- > Posted viahttp://www.ruby-forum.com/. I'm very new at Ruby, but that code works for me: -------------------------------------------- #!/usr/bin/ruby def GetUniqueTextValues(sText) aTags = Array.new puts sText aTags = sText.split puts sText return aTags end a = GetUniqueTextValues('mary had a little lamb') a.each { |x| puts x } -------------------------------------------- prints: mary had a little lamb mary had a little lamb mary had a little lamb -------------------------------------------- I'm using: ruby 1.8.6 (2007-06-07 patchlevel 36) [i586-linux] in OpenSuSE Linux 10.3, kernel 2.6.22.19-0.2 It works the same without the Array.new line.