I'm new to Ruby and a bit confused about how gsub works. I've read the
documentation, searched ruby-forum, and tried numerous google searches,
but can't seem to figure out how to do something that should be simple.
This is just an example, but say I have a string "apple cat flower". I'd
like to replace all instances of the pattern "apple" with the string
"fruit," "cat" with the string "animal," and "flower" with the string
"plant." Meaning I have more than 1 pattern to replace, each with a
specific string to replace.
So I want "apple cat flower" to be converted into "fruit animal plant".
I hope this isn't too confusing. Help is appreciated
There might be a 'smarter' way of doing this: I'm only just learning
Ruby myself , but I know from other domains ("gsub" is an old 'awk'
command for instance) that generally speaking when doing regex
search/replace stuff you would tend to actually do this in "n"
iterations rather than coming up with a complex 'or' regex....
Also, I know in regex you can use the '&' character to mean "The
resulting matched character(s)" - but in this case you are literally
looking for three distinct patterns....so I'm not sure this is even a
possibility...
The above is just my opinion about using regexes - I would certainly
break this up into three separate gsubs: and I might be missing a trick
or two here..so wait for other posts I would say...
You could write yourself a little 'method' here which took two arrays
perhaps, and then iterate over one with the other ? dunno...
John
Chealsea S. wrote:
···
I'm new to Ruby and a bit confused about how gsub works. I've read the
documentation, searched ruby-forum, and tried numerous google searches,
but can't seem to figure out how to do something that should be simple.
This is just an example, but say I have a string "apple cat flower". I'd
like to replace all instances of the pattern "apple" with the string
"fruit," "cat" with the string "animal," and "flower" with the string
"plant." Meaning I have more than 1 pattern to replace, each with a
specific string to replace.
So I want "apple cat flower" to be converted into "fruit animal plant".
I hope this isn't too confusing. Help is appreciated
I'm new to Ruby and a bit confused about how gsub works. I've read the
documentation, searched ruby-forum, and tried numerous google searches,
but can't seem to figure out how to do something that should be simple.
This is just an example, but say I have a string "apple cat flower". I'd
like to replace all instances of the pattern "apple" with the string
"fruit," "cat" with the string "animal," and "flower" with the string
"plant." Meaning I have more than 1 pattern to replace, each with a
specific string to replace.
So I want "apple cat flower" to be converted into "fruit animal plant".
I hope this isn't too confusing. Help is appreciated
--
use.inject do |as, often| as.you_can - without end
On Mon, Sep 1, 2008 at 2:09 PM, Chealsea S. <marikoc@gmail.com> wrote:
I'm new to Ruby and a bit confused about how gsub works. I've read the
documentation, searched ruby-forum, and tried numerous google searches,
but can't seem to figure out how to do something that should be simple.
This is just an example, but say I have a string "apple cat flower". I'd
like to replace all instances of the pattern "apple" with the string
"fruit," "cat" with the string "animal," and "flower" with the string
"plant." Meaning I have more than 1 pattern to replace, each with a
specific string to replace.
So I want "apple cat flower" to be converted into "fruit animal plant".
I hope this isn't too confusing. Help is appreciated