|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
I don't understand this.
irb(main):002:0> '\'' => "'" irb(main):003:0> '\\' => "\\" irb(main):004:0> I know the backslash escapes a character, so in the first line, I escape the quote so it will return a string that is a single quote, but in the second one I would expect it to return "\", instead it returns "\\" both backslashes, and I only want one of them. My actual problem looks like this: irb(main):004:0> "(G\\D01=Name~\D02=1234~\\" => "(G\\D01=Name~D02=1234~\\" The string that is returned is wrong,but if I do irb(main):005:0> "(G\\D01=Name~\\D02=1234~\\" => "(G\\D01=Name~\\D02=1234~\\" the string that is returned is still wrong. ~Jeremy -- Posted via http://www.ruby-forum.com/. |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
In Ruby, there is a distinction between strings that are between
double quotes and strings in single quotes. "\\" escapes the necessary characters, and also allows substitution. ex: "#{name}" => "Pradeep" '\\' does not do any of these. ex: '#{name}' => '#{name}' Single-quoted strings are faster than double-quoted strings. - Pradeep On Nov 6, 2007, at 8:00 PM, Jeremy Woertink wrote: > I don't understand this. > > > irb(main):002:0> '\'' > => "'" > irb(main):003:0> '\\' > => "\\" > irb(main):004:0> > > > I know the backslash escapes a character, so in the first line, I > escape > the quote so it will return a string that is a single quote, but in > the > second one I would expect it to return "\", instead it returns "\\" > both > backslashes, and I only want one of them. My actual problem looks like > this: > > irb(main):004:0> "(G\\D01=Name~\D02=1234~\\" > => "(G\\D01=Name~D02=1234~\\" > > The string that is returned is wrong,but if I do > irb(main):005:0> "(G\\D01=Name~\\D02=1234~\\" > => "(G\\D01=Name~\\D02=1234~\\" > > the string that is returned is still wrong. > > > ~Jeremy > -- > Posted via http://www.ruby-forum.com/. > |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
On Nov 6, 7:00 pm, Jeremy Woertink <jeremywoert...@gmail.com> wrote:
> I don't understand this. > > irb(main):002:0> '\'' > => "'" > irb(main):003:0> '\\' > => "\\" > irb(main):004:0> > > I know the backslash escapes a character, so in the first line, I escape > the quote so it will return a string that is a single quote, but in the > second one I would expect it to return "\", instead it returns "\\" both > backslashes, and I only want one of them. My actual problem looks like > this: > > irb(main):004:0> "(G\\D01=Name~\D02=1234~\\" > => "(G\\D01=Name~D02=1234~\\" > > The string that is returned is wrong,but if I do > irb(main):005:0> "(G\\D01=Name~\\D02=1234~\\" > => "(G\\D01=Name~\\D02=1234~\\" > > the string that is returned is still wrong. > > ~Jeremy > -- > Posted viahttp://www.ruby-forum.com/. If you actually output the string: > puts '\\' \ => nil you should get the result you're expecting. |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
On 11/6/07, Jeremy Woertink <jeremywoertink@gmail.com> wrote:
> I don't understand this. > > > irb(main):002:0> '\'' > => "'" > irb(main):003:0> '\\' > => "\\" > irb(main):004:0> > > > I know the backslash escapes a character, so in the first line, I escape > the quote so it will return a string that is a single quote, but in the > second one I would expect it to return "\", instead it returns "\\" both > backslashes, and I only want one of them. My actual problem looks like > this: > > irb(main):004:0> "(G\\D01=Name~\D02=1234~\\" > => "(G\\D01=Name~D02=1234~\\" > > The string that is returned is wrong,but if I do > irb(main):005:0> "(G\\D01=Name~\\D02=1234~\\" > => "(G\\D01=Name~\\D02=1234~\\" > > the string that is returned is still wrong. s = '\\' puts '\\' #returns \ s.length #returns 1 It's one byte of value 134 in base 10. What you are seeing is the representation of it in irb. Like try... s = "hello " Note the return character before the second end quote. Todd |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
yermej wrote:
> On Nov 6, 7:00 pm, Jeremy Woertink <jeremywoert...@gmail.com> wrote: >> second one I would expect it to return "\", instead it returns "\\" both >> the string that is returned is still wrong. >> >> ~Jeremy >> -- >> Posted viahttp://www.ruby-forum.com/. > > If you actually output the string: > >> puts '\\' > \ > => nil > > you should get the result you're expecting. Rock on. So basically I had to do \\\\ just to get \\ and \\ just to get \. Crazy, but it works so I'm happy. Thanks ~Jeremy -- Posted via http://www.ruby-forum.com/. |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
On 11/6/07, Jeremy Woertink <jeremywoertink@gmail.com> wrote:
> yermej wrote: > > On Nov 6, 7:00 pm, Jeremy Woertink <jeremywoert...@gmail.com> wrote: > >> second one I would expect it to return "\", instead it returns "\\" both > >> the string that is returned is still wrong. > >> > >> ~Jeremy > >> -- > >> Posted viahttp://www.ruby-forum.com/. > > > > If you actually output the string: > > > >> puts '\\' > > \ > > => nil > > > > you should get the result you're expecting. > Rock on. > > So basically I had to do \\\\ just to get \\ and \\ just to get \. > Crazy, but it works so I'm happy. > Thanks Right. When irb shows you "\\", what it's showing you is the double-quoted correct representation of a single backslash. Todd |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
Jeremy Woertink wrote:
> yermej wrote: > >> On Nov 6, 7:00 pm, Jeremy Woertink <jeremywoert...@gmail.com> wrote: >> >>> second one I would expect it to return "\", instead it returns "\\" both >>> the string that is returned is still wrong. >>> >>> ~Jeremy >>> -- >>> Posted viahttp://www.ruby-forum.com/. >>> >> If you actually output the string: >> >> >>> puts '\\' >>> >> \ >> => nil >> >> you should get the result you're expecting. >> > Rock on. > > So basically I had to do \\\\ just to get \\ and \\ just to get \. > Crazy, but it works so I'm happy. > Thanks > > > ~Jeremy > Decent explanation here: http://blade.nagaokaut.ac.jp/cgi-bin...by-talk/240303 -Justin |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
On Nov 6, 6:28 pm, Pradeep Elankumaran <skyfallsin...@gmail.com>
wrote: > In Ruby, there is a distinction between strings that are between > double quotes and strings in single quotes. > "\\" escapes the necessary characters, and also allows substitution. > ex: "#{name}" => "Pradeep" > '\\' does not do any of these. ex: '#{name}' => '#{name}' > > Single-quoted strings are faster than double-quoted strings. I know it seems like that should be the case, but can you provide any proof that it is? In all the tests I've done, I've never found single- quoted strings to be any bit measurably faster than double-quoted. |
|
![]() |
| Outils de la discussion | |
|
|