# Split doesn't work

**URL:** <https://www.ruby-forum.com/t/split-doesnt-work/190100>\
**Category:** Ruby\
**Created:** [June 15, 2010, 12:19am UTC](https://www.ruby-forum.com/t/split-doesnt-work/190100 "2010-06-15T00:19:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jenlindner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e47c2d/32.png) [@jenlindner](https://www.ruby-forum.com/u/jenlindner)\
**Post date:** [June 15, 2010, 12:19am UTC](https://www.ruby-forum.com/t/split-doesnt-work/190100/1 "2010-06-15T00:19:17Z")

</div>

I have code:

\<% @terms.each do |term| %\>\>

\<%regex = Regexp.new(/#{term}/)%\>  
\<%matchdata = regex.match(@def\_str)%\>  
\<%if matchdata%\>

```
<% @def = @def.split(/#{term}/) %>
<% @def.shift %>

```

\<%end%\>  
\<%end%\>

I want to be able to take a list of terms and a string of text (a  
definition) and find the terms in the text. If a term is found in the  
definition, the program is supposed to split the definition on that term  
and then discard everything in the definition before the term was found.

So if I have terms: cuddly, soft and cute

and the def = “teddy bears are cuddly stuffed animals which are  
generally soft and given to cute children”

my program should change def so that after checking for cuddly, def  
looks like:

“stuffed animals which are generally soft and given to cute children”

and after it checks for soft, def should become “and given to cute  
children”

and so on.

The splitting and shifting works for the first term on @terms(which is  
an array)  
but if a second term is found which matches a word in the definition,  
def is not split on that word and the whole definition string is  
shifted.

Can anyone tell me why split is not working the second time a matching  
term is found?

thanks

---

<div class="post-metadata">

**Author:** ![jenlindner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e47c2d/32.png) [@jenlindner](https://www.ruby-forum.com/u/jenlindner)\
**Post date:** [June 15, 2010, 10:29pm UTC](https://www.ruby-forum.com/t/split-doesnt-work/190100/2 "2010-06-15T22:29:51Z")

</div>

Firstly, I suggest you write this as a standalone ruby program without  
erb, so it’s easy to debug. Secondly, add debugging print statements to  
show what the variables are doing at each stage.

Your code uses both @def and @def\_str, so I’m having to guess what  
you’re trying to do, but I imagine something like this:

@terms = %w(cuddly soft cute)  
@def\_str = “teddy bears are cuddly stuffed animals which are generally  
soft and given to cute children”  
@def = @def\_str  
@terms.each do |term|  
regex = Regexp.new(/#{term}/)  
matchdata = regex.match(@def\_str)  
if matchdata  
puts “Match!”  
puts “Before: @def = #{@def.inspect}”

---

<div class="post-metadata">

**Author:** ![jenlindner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e47c2d/32.png) [@jenlindner](https://www.ruby-forum.com/u/jenlindner)\
**Post date:** [June 16, 2010, 5:34pm UTC](https://www.ruby-forum.com/t/split-doesnt-work/190100/3 "2010-06-16T17:34:27Z")

</div>

Brian C. wrote:

> Firstly, I suggest you write this as a standalone ruby program without  
> erb, so it’s easy to debug. Secondly, add debugging print statements to  
> show what the variables are doing at each stage.
> 
> Your code uses both @def and @def\_str, so I’m having to guess what  
> you’re trying to do, but I imagine something like this:
> 
> @terms = %w(cuddly soft cute)  
> @def\_str = “teddy bears are cuddly stuffed animals which are generally  
> soft and given to cute children”  
> @def = @def\_str  
> @terms.each do |term|  
> regex = Regexp.new(/#{term}/)  
> matchdata = regex.match(@def\_str)  
> if matchdata  
> puts “Match!”  
> puts “Before: @def = #{@def.inspect}”

Thanks for your help Brian I figured out that I was telling def to  
split, but I forgot to make sure it was returned to string form after  
the first split. 🙂

---

<div class="post-metadata">

**Author:** ![jenlindner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e47c2d/32.png) [@jenlindner](https://www.ruby-forum.com/u/jenlindner)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/split-doesnt-work/190100/4 "2010-06-17T16:22:05Z")

</div>

On Tue, Jun 15, 2010 at 7:19 AM, Jennifer L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> \<%end%\>  
> \<%end%\>

Hi,

> \<% @def.shift %\>  
> This line is run only and doesn’t output value.

So change this line to: \<%= @def.shift %\>  
to solve this problem.

Thanks.

---

<div class="post-metadata">

**Author:** ![jenlindner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e47c2d/32.png) [@jenlindner](https://www.ruby-forum.com/u/jenlindner)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/split-doesnt-work/190100/5 "2010-06-17T16:22:49Z")

</div>

Sora’s suggestion works, but doesn’t explain what is going on. The  
reason is that the variable (@def) is a string the first time around,  
but after the first split it becomes an array because that’s what  
split returns. You can convert it back to a string after the shift  
with to\_s or join, like so:

definition = definition.to\_s

or

definition = definition.join(’ ')

Adding a ‘=’ essential calls to\_s on the array.

By the way, def is a reserved keyword, I would avoid using it as a  
variable name, especially since there are better choices.

HTH,  
Ammar
