Forum: Ruby on Rails String#split works w/ Regexp in IRB, not in rails

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.
236e5f67c35a7db58901b2ac90f49b4f?d=identicon&s=25 JT Kimbell (studbucket)
on 2007-04-23 23:40
I'm trying to take a string in the format "word[field] and
word2[field2]...", etc.  I have gotten this to work when splitting it in
IRB using the following syntax:

tempstring.split(/\[|\]/)

which returns:

["word", "field", "word2", "field2",...]

However, when I attempt to make the exact same call in a rails
controller, I get the following error message on the line where
String#split is called.

"can't convert Regexp into String"

However, I don't know why it should be trying to convert my Regexp into
a string, when the API shows you can use strings or regular expressions.
--> http://www.ruby-doc.org/core/classes/String.html#M000818

If you can't figure it out from what I have given and need more code to
look at, let me know.

Thanks,

JT
2f9a03aa0fcfe945229cb6126eda2cb2?d=identicon&s=25 Philip Hallstrom (Guest)
on 2007-04-23 23:53
(Received via mailing list)
> I'm trying to take a string in the format "word[field] and
> word2[field2]...", etc.  I have gotten this to work when splitting it in
> IRB using the following syntax:
>
> tempstring.split(/\[|\]/)

Maybe... tempstring.split(/[\]\[]/)

I can't think of a why irb likes yours, but not rails though...
72ea925c0ca3d19fdd2f12fa76681624?d=identicon&s=25 Stephan Wehner (stephanwehner)
on 2007-04-24 00:33
JT Kimbell wrote:
> I'm trying to take a string in the format "word[field] and
> word2[field2]...", etc.  I have gotten this to work when splitting it in
> IRB using the following syntax:
>
> tempstring.split(/\[|\]/)
>
> which returns:
>
> ["word", "field", "word2", "field2",...]
>
> However, when I attempt to make the exact same call in a rails
> controller, I get the following error message on the line where
> String#split is called.

How about you post the code?

Maybe tempstring happens to be a regexp for some reason.

Stepohan
236e5f67c35a7db58901b2ac90f49b4f?d=identicon&s=25 JT Kimbell (studbucket)
on 2007-04-26 18:20
Stephan Wehner wrote:
> How about you post the code?
>
> Maybe tempstring happens to be a regexp for some reason.
>
> Stepohan

Hi,

Here is the pertinent code:

----
@querytemp = params[:content_query]
#The following code queries the pubmed site and gets their expanded
query, while removing their markups.
sh = Shell.new();
@querytemp2 = sh.system('getdetailquery', @querytemp)
@temparray = @querytemp2.split(/\[|\]/)  #Split the string at brackets (
[ and ] )
----

getdetailquery is perl script that expands the query for me, and I can
guarantee it works, I've tested it in irb, ruby programs, and by itself.

All of those variables are used for the first time in that code, so you
are seeing them when they are 'initialized'.  The error is thrown on the
line where the split method is being called.

One the line above it, I tried hard-coding the value 'flu' in for
querytemp, and I still get the same error.  It doesn't like me passing a
Regexp instead of a string for that :(

Thanks,

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