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.

···

--
Posted via http://www.ruby-forum.com/.

Hi,

Michael Sung 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. They only exist for the parsing
process.

···

--
Posted via http://www.ruby-forum.com/\.

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" ?

···

On Wed, Jun 13, 2012 at 6:15 AM, Michael Sung <lists@ruby-forum.com> wrote:

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.

--
Darryl L. Pierce <mcpierce@gmail.com>
"What do you care what people think, Mr. Feynman?"

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

···

--
Posted via http://www.ruby-forum.com/.

Michael Sung 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 ?

···

--
Posted via http://www.ruby-forum.com/\.

minor nitpick:

It's called a "backslash".

···

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

--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]