Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Double quote escape character

Thread Tools

Double quote escape character

Alvaro Perez
Posts: n/a
Austin Ziegler wrote:
> On 8/16/07, Alvaro Perez <(E-Mail Removed)> wrote:
>> It�s a bit strange this.
>> On my irb:
>> irb(main):053:0> p "\"hello\""
>> "\"hello\""

> puts "\"hello\"" # => "hello"
> puts "\"\"hello\"\"" # => ""hello""
> p uses #inspect, which usually escapes certain values. IRB uses
> #inspect, too.
> -austin

that´s very interesting, I had already notice that p and puts work
differently, i'll search about that #inspect thing...

Posted via

Reply With Quote
Posts: n/a
On Aug 16, 10:28 am, Alvaro Perez <(E-Mail Removed)> wrote:
> thats very interesting, I had already notice that p and puts work
> differently, i'll search about that #inspect thing...

Let me try to explain this:
@a = "foo"
@a is now a 3-character string (not 5 characters; the double quotes
aren't part of the string).

puts @a.length
#=> 3

puts @a, @a.inspect
#=> foo
#=> "foo"

p @a
#=> "foo"

As Austin said, the 'p' method calls the #inspect method on its
arguments. Inspect tries to show you what type an object is, so (in
the above) it puts double-quotes around the 3 characters in @a to show
you that it's a string.

irb also uses #inspect to show you the value of the last expression on
the line.
irb(main):001:0> @a = "foo"
=> "foo"

See? Those double quotes aren't part of the actual string, they're
just there to show you what's inside the string.

Now, let's create a string that has a double quote character inside
@b = 'foo"bar'
puts @b, @b.length, @b.inspect
#=> foo"bar
#=> 7
#=> "foo\"bar"

The string is exactly 7 characters long. When you call #inspect, it
puts double quotes around the string when showing it to you. And, so
that you know that the double quote in the middle isn't the end of the
string, it shows you a source-code-like representation, a backslash
before the double quote.

This same confusion is seen in this thread already. When you wrote:
...there's no possible way to write in Ruby a string like this:
what did you mean? A 7 character string with double quotes at either
end? Or a 9 character string with two double quotes at each end?

As Austin showed, there are plenty of ways to create either. You just
need to not confuse the contents of the string what irb and #inspect
are showing you in their attempt to be clear about the contents of the

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with single-quote and double-quote when using subprocess. Evan Python 3 11-04-2008 12:24 PM
Escape character in java How can use double quote in string Sanjeev Java 3 05-03-2008 06:59 PM
Datagrid on load; replace all double single quote to single quote to display to user Eric Layman ASP .Net 3 04-14-2007 07:16 AM
Single Quote Versus Double Quote In A href link HTML 3 06-13-2006 12:42 AM
Javascript double quote escape character? Alden Streeter ASP General 1 11-26-2003 09:45 PM