Working with strings...

T

Tom Rathbone

Hi,

Is there a function that would work over the characters in String in
the way that each works for an array? I want to process the string
character by character and thought that #each would do this,
apparently not. Even better would be a collect style method.
Something like this.

"some string".collect do |x|
x + '|'
end

#collect_char or #each_char would be great.

Perhaps not the best example there, basically i intend to interpret a
string of character commands from a string i.e. something like
Brainf**k.

Thanks,

Tom.
 
S

Szymon Drejewicz

Try this:

result = ''
"some string".each_byte { |b| result << b << '|' }
puts result
 
S

Sea&Gull

Tom said:
Hi,

Is there a function that would work over the characters in String in
the way that each works for an array? I want to process the string
character by character and thought that #each would do this,
apparently not. Even better would be a collect style method.
Something like this.

"some string".collect do |x|
x + '|'
end




#collect_char or #each_char would be great.

"some string".split('').collect {|i| print i, '|'}
 
F

Florian Gross

Tom said:
Is there a function that would work over the characters in String in
the way that each works for an array? I want to process the string
character by character and thought that #each would do this,
apparently not. Even better would be a collect style method.
Something like this.

"some string".collect do |x|
x + '|'
end

Generally you can do "some string".scan(/./).map { |x| x + "|" }

But note that will construct an intermediate array which is a little
inefficient -- if you have really big strings (a few hundred kilobyte)
you're probably better off doing it like this instead:

result = ""; "some string".scan(/./) { |x| result << x << "|" }; result
 
S

Szymon Drejewicz

string.gsub!(/./,'\&|')


cool :) but it could be even shorter:

string.gsub! //,'\0|'

:)))

(one leading '|' in output more)
 
D

Douglas Livingstone

(one leading '|' in output more)

Better to rename the var >:)

s.gsub!(/./,'\&|')
 
D

Daniel Berger

Tom said:
Hi,

Is there a function that would work over the characters in String in
the way that each works for an array? I want to process the string
character by character and thought that #each would do this,
apparently not. Even better would be a collect style method.
Something like this.

"some string".collect do |x|
x + '|'
end


#collect_char or #each_char would be great.

Perhaps not the best example there, basically i intend to interpret a
string of character commands from a string i.e. something like
Brainf**k.

Thanks,

Tom.

s = "some string"

s.split('').join('|') + '|'
# or
s.unpack("A"*s.length).join('|') + '|'

Regards,

Dan
 

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,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top