Working with strings

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.

Try this:

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

···

--
Szy

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

Or:

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

···

--
Szy

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.

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

or:

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

···

On Thu, 3 Feb 2005, Szymon Drejewicz wrote:

Or:

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

--
Wybo

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

cool :slight_smile: but it could be even shorter:

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

:-)))

(one leading '|' in output more)

···

--
Szy

(one leading '|' in output more)

Better to rename the var >:)

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

string.gsub! //,'|'

     Hugh

···

On Thu, 3 Feb 2005, Szymon Drejewicz wrote:

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

:-)))

(one leading '|' in output more)