a = "a''s" puts a.sub(/''/,"\\''") I would expect the above program to display a\''s but instead it displays ass Is there a logical explanation? _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
On Thursday 08 September 2005 21:50, Bogdan Ionescu wrote:> a = "a''s" > puts a.sub(/''/,"\\''")Try puts a.sub(/''/, "\\\\''") or puts a.sub(/''/, %q{\\\''}) \'' is a meta character denoting the string following a successful match. Michael -- Michael Schuerig They tell you that the darkness mailto:michael-q5aiKMLteq4b1SvskN2V4Q@public.gmane.org Is a blessing in disguise http://www.schuerig.de/michael/ --Janis Ian, From Me To You
Some character sequences from replacement string have special meaning for ruby. One would be \'' that returns the part of the string after the match. Now \ is interpreted as an escape sequence by the lexer so \\'' becomes \''. What you probably want is a.sub(/''/,''\'''') that does the "right" thing. Zsombor Bogdan Ionescu wrote:> a = "a''s" > puts a.sub(/''/,"\\''") > I would expect the above program to display a\''s but instead it displays ass > Is there a logical explanation? > > > ------------------------------------------------------------------------ > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails-- Company - http://primalgrasp.com Thoughts - http://deezsombor.blogspot.com
Bogdan Ionescu <bogdan.ionescu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes:> a = "a''s" > puts a.sub(/''/,"\\''") > I would expect the above program to display a\''s but instead it displays ass > Is there a logical explanation?That''s an odd thing to be sure. However, a.sub(/''/,''\'''') works as you''d expect. -- Doug Alcorn - http://lathi.net/RubyOnRailsDeveloper doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org
On 9/8/05, Bogdan Ionescu <bogdan.ionescu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> a = "a''s" > puts a.sub(/''/,"\\''") > I would expect the above program to display a\''s but instead it displays > ass > Is there a logical explanation?\'' is a backslash sequence that returns the string after the match. Since ''s'' is after the match, that''s what you''re getting. You could try this: a.sub(/''/, ''\\\\\&'') \& returns the match. The many backslashes are there to account for Rubys two passes through the string.
It works as expected if you''re expecting it to return the same string On 9/8/05, Doug Alcorn <doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org> wrote:> > Bogdan Ionescu <bogdan.ionescu-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> writes: > > > a = "a''s" > > puts a.sub(/''/,"\\''") > > I would expect the above program to display a\''s but instead it displays > ass > > Is there a logical explanation? > > That''s an odd thing to be sure. However, a.sub(/''/,''\'''') works as > you''d expect. > -- > Doug Alcorn - http://lathi.net/RubyOnRailsDeveloper > doug-jGAhs73c5XxeoWH0uzbU5w@public.gmane.org > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails