7stud2
(7stud --)
27 November 2013 12:42
1
I'm learning to program and I want to insert the user response to a
question but put " " around it. Like this:
Gets = reply
Puts "WHAT DO YOU MEAN " + "reply.upcase" + " YOU ARE FIRED!!"
any ideas how to escape " without using string interpolation? I'm
reading a book that hasn't introduced interpolation yet so want to keep
it simple.
···
--
Posted via http://www.ruby-forum.com/ .
(I'm not sure what has been introduced in the book yet, so these questions might not be appropriate!)
Do you know about single quotes for strings yet?
puts 'Here is a double quote: "'
Do you know about how \ can be used to escape characters or introduce "special" characters in a string literal?
puts "Here is a double quote: \""
Hope this helps,
Mike
···
On Nov 27, 2013, at 7:42 AM, Joshua P. <lists@ruby-forum.com> wrote:
I'm learning to program and I want to insert the user response to a
question but put " " around it. Like this:
Gets = reply
Puts "WHAT DO YOU MEAN " + "reply.upcase" + " YOU ARE FIRED!!"
any ideas how to escape " without using string interpolation? I'm
reading a book that hasn't introduced interpolation yet so want to keep
it simple.
--
Mike Stok <mike@stok.ca>
http://www.stok.ca/~mike/
The "`Stok' disclaimers" apply.
7stud2
(7stud --)
27 November 2013 12:52
3
If you want to add double quote characters, you have a lot of options.
Here are a few of them without using Interpolation.
puts 'WHAT DO YOU MEAN "' + reply.upcase + '" YOU ARE FIRED!!'
puts "WHAT DO YOU MEAN " + '"' + reply.upcase + '"' + " YOU ARE FIRED!!"
puts "WHAT DO YOU MEAN \"" + reply.upcase + "\" YOU ARE FIRED!!"
puts %q|WHAT DO YOU MEAN "| + reply.upcase + %q|" + " YOU ARE FIRED!!|
···
--
Posted via http://www.ruby-forum.com/ .
7stud2
(7stud --)
27 November 2013 14:52
4
Oops, that last one should be
puts %q|WHAT DO YOU MEAN "| + reply.upcase + %q|" YOU ARE FIRED!!|
···
--
Posted via http://www.ruby-forum.com/ .
7stud2
(7stud --)
28 November 2013 17:16
5
If you are not embedding code inside any string just don't use double
quotes, it is more expensive for the system, because with double quotes
Ruby has to look for #{} and some representations like \n\t\s, etc. In
this case just use simple quotes, so Ruby takes what you put inside as
literal. To escape the single literal just \'.
I recommend you to use the first example of Joel.
···
--
Posted via http://www.ruby-forum.com/ .
7stud2
(7stud --)
28 November 2013 17:34
6
Thanks guys, this info worked a treat!
···
--
Posted via http://www.ruby-forum.com/ .