split doesn't work

J

Jennifer Lee

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
 
S

Sora Harakami

I have code:

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

=A0<%regex =3D Regexp.new(/#{term}/)%>
=A0<%matchdata =3D regex.match(@def_str)%>
=A0<%if matchdata%>

=A0 =A0<% =A0@def =3D @def.split(/#{term}/) %>
=A0 =A0<% [email protected] %>

=A0<%end%>
<%end%>
Hi,

=A0 =A0<% [email protected] %>
This line is run only and doesn't output value.

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

Thanks.

--=20
Sora Harakami - @sora_h
Blog: http://codnote.net/
Detail: http://sorah.cosmio.net/
 
A

Ammar Ali

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 =3D definition.to_s

or

definition =3D definition.join(' ')

Adding a '=3D' 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

 
B

Brian Candler

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}"
 
J

Jennifer Lee

Brian said:
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. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top