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.
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.
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('|') + '|'