Weird gsub behaviour

ruby 1.8.6 (2007-09-24 patchlevel 111)

str = '\&123'
puts "abc".gsub("b", str)
puts "abc".gsub("b", "#{str}")
puts "abc".gsub("b", str.to_s)
puts "abc".gsub("b", '\&123')
puts "abc".gsub("b", "\&123")

ruby test.rb

ab123c
ab123c
ab123c
ab123c
a&123c <------------------- This I want to achieve using temporary
variable

Exit code: 0

If I change
str = '\&123'
to
str = "\&123"
it works fine, but I get str from "match" function, so I cannot
specify it manually within parantheses.... Is there any way to change
string's 'string' to "string" behaviour???

The exact problem

a = '<template><aaa:bbb/></template>'
b = '<a class="add_line" href="#" id="add_fields_invoice_lines"
onclick="add_fields(this, "invoice_lines", "&lt;fieldset class=
\&quot;inputs invoice_line\&quot;&gt;&lt;ol&gt;\n &lt;li class=
\&quot;string required invoice_line_name\&quot; id=\&quot;"'

str = (b.match /.*/)[0]

puts str

pn="bbb"

puts a.gsub("<aaa:#{pn}\/>", str)

=> <template><a class="add_line" href="#"
id="add_fields_invoice_lines" onclick="add_fields(this,
"invoice_lines", "&lt;fieldset class=<aaa:bbb/>quot;inputs
invoice_line<aaa:bbb/>quot;&gt;&lt;ol&gt;\n &lt;li class=<aaa:bbb/

Not sure I understand. From

a&123c <------------------- This I want to achieve using temporary

I ask, do you know that & does not need escaping?

"abc".gsub("b", "&123") # => "a&123c"

Why are you escaping the & at all ?

str = '&123'
puts "abc".gsub("b", str)

outputs a&123c for me

Fred

It was received from already escaped html mixed with javascript - that
is why :frowning:
I have no influence on the fact that these slashes are there...

Anyway I have came up with a solution - maybe there is a simpler way,
however the code below works

    str = '\&123'

    puts "abc".gsub("b", str.gsub(/\&/o, '\\\&\2\1'))

    => a\&123c

Argh - slashes missing -> here is the pastie code -> http://pastie.org/832493

Easier:

    "abc".gsub("b") { |_| str }

There str is taken verbatim.

The exact problem

a = '<template><aaa:bbb/></template>'
b = '<a class="add_line" href="#" id="add_fields_invoice_lines"
onclick="add_fields(this, "invoice_lines", "&lt;fieldset class=
\&quot;inputs invoice_line\&quot;&gt;&lt;ol&gt;\n &lt;li class=
\&quot;string required invoice_line_name\&quot; id=\&quot;"'

str = (b.match /.*/)[0]

puts str

pn="bbb"

puts a.gsub("<aaa:#{pn}\/>", str)

So did you try:
a.gsub("<aaa:#{pn}\/>") { b }

-Rob

=> <template><a class="add_line" href="#"
id="add_fields_invoice_lines" onclick="add_fields(this,
"invoice_lines", "&lt;fieldset class=<aaa:bbb/>quot;inputs
invoice_line<aaa:bbb/>quot;&gt;&lt;ol&gt;\n &lt;li class=<aaa:bbb/

quot;string required invoice_line_name<aaa:bbb/>quot; id=<aaa:bbb/
quot;"</template>

ruby 1.8.6 (2007-09-24 patchlevel 111)

str = '\&123'
puts "abc".gsub("b", str)
puts "abc".gsub("b", "#{str}")
puts "abc".gsub("b", str.to_s)
puts "abc".gsub("b", '\&123')
puts "abc".gsub("b", "\&123")

ruby test.rb

ab123c
a&123c <------------------- This I want to achieve using temporary
variable

Exit code: 0

If I change
str = '\&123'
to
str = "\&123"
it works fine, but I get str from "match" function, so I cannot
specify it manually within parantheses.... Is there any way to change
string's 'string' to "string" behaviour???

-- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com

No, I did not... and it sure works! I knew that it should be easier :slight_smile:

Thank You for another piece of knowledge, Rob and Xavier