Forum: Ruby String.split crashes my program

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
C5448702e5a55e59175aed325cd6bff0?d=identicon&s=25 Tomas Na (tomast)
on 2009-04-06 16:22
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?
9b3d14efbedc70a652b441b2ec6b871b?d=identicon&s=25 Loga Ganesan (loganathan1)
on 2009-04-06 16:33
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?


Its working for me. But I couldn't get your actual problem. If you give
me further details as like your code, your ruby version, It would be
useful for me
to track the problem.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-04-06 16:41
(Received via mailing list)
On 06.04.2009 16:21, 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.

Well, you could do that in one go:

tags = Set.new
...

s_text.scan(/\S{5,}/) {|match| tags << match}

> 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)

Convention in Ruby is to use lower case names for methods and also
separate words by underscores.  This avoids confusing methods with
classes.

def get_unique_text_values

>   aTags = Array.new

That Array creation is superfluous.

>   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.

This is not exactly what you called a "crash" unless the process would
terminate.  Does it?  If not, how long did you wait for the split to
complete?  What version of Ruby are you using and what is your input
String - especially how long is it?

> 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?

Please provide more information.

Kind regards

  robert
570f9ad0ade7ac63b0a75cdb78939041?d=identicon&s=25 Mark S Bilk (Guest)
on 2009-04-06 16:55
(Received via mailing list)
On Apr 6, 7:21 am, Tomas Na <kraz...@hotmail.com> wrote:
>   puts sText
> 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.
C5448702e5a55e59175aed325cd6bff0?d=identicon&s=25 Tomas Na (tomast)
on 2009-04-06 17:14
It reaches a block of code which causes it to become unresponsive. That
to me indicates a crash. Maybe not to you, but this discussion isn't
about semantics.

Here is my output:
C:\Users\Tomas\Ruby>ruby test.rb
Split this text

Then nothing. We sit here for ever while it "hasnt" crashed.

My Ruby version is:
ruby 1.8.6 (2007-09-24 patchlevel 111) [i386-mswin32]
Running on Windows 7


The other split that works is:
def ProcessHeaders(sHead)
    hHeaders = {}
    sHead.each{|line|
  saHeadParts = line.split(/: /)
  hHeaders[saHeadParts[0]]=saHeadParts[1]
    }
    return hHeaders
end

For processing HTTP headers.

I appreciate that my Ruby coding conventions may not match the standard
but I have been programming Ruby for only a day so far and I came here
from C++, so I was just sticking to what I know. That can be refactored
later, I'd rather have a working program than a conventional one.

I tried your scan example which did what I was trying to do, so if all
else fails I will use that. Thanks for the help.
289cf19aa581c445915c072bf45c5e25?d=identicon&s=25 Todd Benson (Guest)
on 2009-04-06 17:58
(Received via mailing list)
On Mon, Apr 6, 2009 at 10:14 AM, Tomas Na <krazz17@hotmail.com> wrote:
> It reaches a block of code which causes it to become unresponsive. That
> to me indicates a crash. Maybe not to you, but this discussion isn't
> about semantics.
>
> Here is my output:
> C:\Users\Tomas\Ruby>ruby test.rb
> Split this text
>
> Then nothing. We sit here for ever while it "hasnt" crashed.

The fact that you are not even getting to that first puts suggests you
are never calling the method.

Todd
C5448702e5a55e59175aed325cd6bff0?d=identicon&s=25 Tomas Na (tomast)
on 2009-04-06 18:10
Todd Benson wrote:
> On Mon, Apr 6, 2009 at 10:14 AM, Tomas Na <krazz17@hotmail.com> wrote:
>> It reaches a block of code which causes it to become unresponsive. That
>> to me indicates a crash. Maybe not to you, but this discussion isn't
>> about semantics.
>>
>> Here is my output:
>> C:\Users\Tomas\Ruby>ruby test.rb
>> Split this text
>>
>> Then nothing. We sit here for ever while it "hasnt" crashed.
>
> The fact that you are not even getting to that first puts suggests you
> are never calling the method.
>
> Todd


My text was "Split this text" - That is the first puts being executed.
570f9ad0ade7ac63b0a75cdb78939041?d=identicon&s=25 Mark S Bilk (Guest)
on 2009-04-06 18:20
(Received via mailing list)
On Apr 6, 8:14 am, Tomas Na <kraz...@hotmail.com> wrote:
> It reaches a block of code which causes it to become unresponsive. That
> to me indicates a crash. Maybe not to you, but this discussion isn't
> about semantics.
>
> Here is my output:
> C:\Users\Tomas\Ruby>ruby test.rb
> Split this text
>
> Then nothing. We sit here for ever while it "hasnt" crashed.

So "Split this text" is the string you're scanning?
Seems like the program is in a loop, rather than crashed.
Would you please copy and paste the entire exact content
of test.rb into a Usenet article in between two lines
of dashes ("-------------")?
Even better would be to zip it and put it on a website.
Maybe you have some weird characters in the file.
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2009-04-06 19:15
(Received via mailing list)
On 06.04.2009 18:15, Mark S Bilk wrote:
>
> So "Split this text" is the string you're scanning?
> Seems like the program is in a loop, rather than crashed.
> Would you please copy and paste the entire exact content
> of test.rb into a Usenet article in between two lines
> of dashes ("-------------")?
> Even better would be to zip it and put it on a website.
> Maybe you have some weird characters in the file.

pastie would work as well.  I agree, we should see the whole program
that is misbehaving, or better the smallest program that exhibits the
described behavior.

Kind regards

  robert
This topic is locked and can not be replied to.