String.chars.to_a question

Hello,

Is there a way to capture " (quotation marks) when you use the
chars.to_a command?

For example:

string = string.chars.to_a
length = string.length
newString = Array.new

while length >= 0
newString << [string[length]]
length = length - 1

end

This works fine for letters, but does not seem to put " (quotation
marks) into an array.

On Wed, Jun 13, 2012 at 6:15 AM, Michael S. [email protected]
wrote:

while length >= 0
newString << [string[length]]
length = length - 1

end

This works fine for letters, but does not seem to put " (quotation
marks) into an array.

There’s nothing special about letters verses punctuation in the above
code. Are you talking about the quotes that surround the string; i.e.,
string = “this is a string” ?

Have you escaped the double-quotation mark " with a back-splash like “I
escape the " with a back splash” ? Or, you can try other delimiters
like quotation mark ’ or use a word array instead, like %w{this is an
word array}.

Hi,

Michael S. wrote in post #1064374:

This works fine for letters, but does not seem to put " (quotation
marks) into an array.

Are you sure? When I write

string = ‘ab"c’
chars = string.chars.to_a
puts char

I get the correct result:

a
b
"
c

Or do you mean the surrounding quotes of string literals in Ruby? Those
are not characters of the string but syntax elements

On Thu, Jun 14, 2012 at 07:08:33PM +0900, Scott W. wrote:

Have you escaped the double-quotation mark " with a back-splash like “I
escape the " with a back splash” ? Or, you can try other delimiters
like quotation mark ’ or use a word array instead, like %w{this is an
word array}.

minor nitpick:

It’s called a “backslash”.

Michael S. wrote in post #1064374:

string = string.chars.to_a
length = string.length
newString = Array.new

while length >= 0
newString << [string[length]]
length = length - 1

end

A few comments…

  1. I’d not call arrays “strings”. It’s confusing. “newString” isn’t a
    string here.
  2. Why is “string[length]” surrounded by “[]” ? This creates an array
    of 1-element arrays. Is this what you want?
  3. you first take the element string[string.length]. This is nil. Is
    this what you want?

Are you trying to implement string.reverse ?