[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Working with strings...

Tom Rathbone

2/3/2005 11:46:00 AM

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

>> "s|o|m|e| |s|t|r|i|n|g|"

#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.


10 Answers

Szymon Drejewicz

2/3/2005 12:07:00 PM

0

Try this:

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

--
Szy

Sea&Gull

2/3/2005 12:11:00 PM

0

Tom Rathbone wrote:
> 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
>
>
>>>"s|o|m|e| |s|t|r|i|n|g|"
>
>
> #collect_char or #each_char would be great.

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


>
> 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&g

Florian Gross

2/3/2005 12:13:00 PM

0

Tom Rathbone wrote:

> 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
>
>
>>>"s|o|m|e| |s|t|r|i|n|g|"

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

Szymon Drejewicz

2/3/2005 1:33:00 PM

0

Or:

string.gsub!(/./) { |match| match += '|' }

--
Szy

Wybo Dekker

2/3/2005 2:04:00 PM

0

Szymon Drejewicz

2/3/2005 2:49:00 PM

0

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


cool :-) but it could be even shorter:

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

:-)))

(one leading '|' in output more)

--
Szy

Douglas Livingstone

2/3/2005 2:58:00 PM

0

> (one leading '|' in output more)

Better to rename the var >:)

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


Hugh Sasse

2/3/2005 3:18:00 PM

0

YANAGAWA Kazuhisa

2/3/2005 4:08:00 PM

0

In Message-Id: <777da1740502030346417566d9@mail.gmail.com>
Tom Rathbone <tom.rathbone@gmail.com> writes:

> "some string".collect do |x|
> x + '|'
> end
>
> >> "s|o|m|e| |s|t|r|i|n|g|"

require "enumerator"

p "some string".enum_for(:each_byte).inject("") {|s, c| s << c.chr << "|"}
#=> "s|o|m|e| |s|t|r|i|n|g|"


--
kjana@dm4lab.to February 4, 2005
Speech is silver, silence is golden.



djberg96

2/3/2005 4:38:00 PM

0

Tom Rathbone wrote:
> 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
>
> >> "s|o|m|e| |s|t|r|i|n|g|"
>
> #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