Strings .each method

I am using .each to pull apart a string at every space. Is there a way
to peek ahead to see what the next substring is without moving to the
next substring in the block?

I have a ruby book and I was searching for this through google but have
had no luck so far. Didn’t know if this was an easy answer for anybody.

Thanks in advance,

Chris

Chris H. wrote:

I am using .each to pull apart a string at every space. Is there a way
to peek ahead to see what the next substring is without moving to the
next substring in the block?

I have a ruby book and I was searching for this through google but have
had no luck so far. Didn’t know if this was an easy answer for anybody.

I am not sure what you mean but here is how to perform the task:

a = “This is a big old string.”
p a.split

=> [“This”, “is”, “a”, “big”, “old”, “string.”]

I’m not sure about looking ahead to the next element using #each, but if
you
can describe why you want to do it, there might be a different way than
using #each.

-Corey

Corey H. wrote:

but if you can describe why you want to do it, there might be a different way

It is kind of hard to explain but I have a long string that someone
typed into a form which was then saved into a database. They used made
up tags such as “, , , , …” I
have to translate the string into html and add javascript etc. based on
the different tags they wrote.

I have it working for everything except one task. In this one task I
want to pass the words between two tags as a value for the javascript.
The problem is it is pulling the phrase apart at every space. I know I
can add .each(???) and have it pull everything apart at the ??? but it
would involve a lot of adding ??? to this long string.

What I am trying to do is add a while loop inside the block that will
look ahead at the next substring to see if it is a “”. If it
is I want it to exit the while loop and go on without throwing away the
“” tag because it is needed next time through the block.

Sorry if that didnt make sense at all.

On Feb 2, 2008, at 5:14 PM, Chris H. wrote:

the different tags they wrote.
it
is I want it to exit the while loop and go on without throwing away
the
“” tag because it is needed next time through the block.

Sorry if that didnt make sense at all.

Perhaps something like this:

irb> require ‘enumerator’
=> true
irb> a = “This is a big list of words”
=> “This is a big list of words”
irb> (a.split(’ ') << nil).each_cons(2) {|word,lookahead| puts “%-10s
%s”%[word.inspect,lookahead.inspect]}
“This” “is”
“is” “a”
“a” “big”
“big” “list”
“list” “of”
“of” “words”
“words” nil
=> nil

Putting the << nil gives the last word something to be spit out of the
each_cons with.

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

irb> (a.split(’ ') << nil).each_cons(2) {|word,lookahead| puts “%-10s
%s”%[word.inspect,lookahead.inspect]}

Awesome. I understand how to use it but what exactly does %-10s %s"%
mean/do?

Thanks a lot for the feedback,

chris

On Feb 2, 2008, at 5:28 PM, Chris H. wrote:

irb> (a.split(’ ') << nil).each_cons(2) {|word,lookahead| puts “%-10s
%s”%[word.inspect,lookahead.inspect]}

Awesome. I understand how to use it but what exactly does %-10s %s"%
mean/do?

Thanks a lot for the feedback,

chris

That’s a way to do formatted output like Kernel.sprintf, but shorter
to type using String#%

%-10s means a string(s) left-justified(-) in a 10 characters wide
space (will use more than 10 if the string to be formatted is longer)

%s means a whole string

-Rob

Rob B. http://agileconsultingllc.com
[email protected]

I am not sure what you mean but here is how to perform the task:

I have something like the following. I tried to make it a little
simpler.

a = “This is a big old string.”

a.each do |w|

I have a bunch of if else statements in this block

end

So w = “This” first time around
w = “is” second time around
w = “a” third time around …

What I was trying to ask is when w = “big” inside this block is there a
way I could peek ahead within the block and see “old” as the next
substring without assigning “old” to w?

Does that make more sense?

Thanks a lot for taking a look,

Chris

Thanks a lot!

Very cool, Rob!

On Sat, 02 Feb 2008 17:14:26 -0500, Chris H. wrote:

I have it working for everything except one task. In this one task I
Sorry if that didnt make sense at all.
Might I suggest that using REXML (or another XML parser) may make more
sense for what you’re trying to do? First navigate your way through the
tag structure, then you can get the text elements that you’re trying to
find.

I think this might.

string = “This is a big old string”

#Instead of iterating you could match text “big”
find = string.match(‘big’)

#to see if “big” is in the next block of the sentence do this.
puts find.post_match.match(‘old’)

Thanks.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs