stevo84
(stevo84)
February 19, 2010, 8:47am
1
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???
stevo84
(stevo84)
February 19, 2010, 10:14am
2
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", "<fieldset class=
\"inputs invoice_line\"><ol>\n <li class=
\"string required invoice_line_name\" id=\""'
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", "<fieldset class=<aaa:bbb/>quot;inputs
invoice_line<aaa:bbb/>quot;><ol>\n <li class=<aaa:bbb/
fxn
(Xavier Noria)
February 19, 2010, 10:24am
3
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
stevo84
(stevo84)
February 19, 2010, 10:46am
5
It was received from already escaped html mixed with javascript - that
is why
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
stevo84
(stevo84)
February 19, 2010, 10:48am
6
Argh - slashes missing -> here is the pastie code -> http://pastie.org/832493
fxn
(Xavier Noria)
February 19, 2010, 10:55am
7
Easier:
"abc".gsub("b") { |_| str }
There str is taken verbatim.
rab
(Rob Biedenharn)
February 19, 2010, 3:08pm
8
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", "<fieldset class=
\"inputs invoice_line\"><ol>\n <li class=
\"string required invoice_line_name\" id=\""'
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", "<fieldset class=<aaa:bbb/>quot;inputs
invoice_line<aaa:bbb/>quot;><ol>\n <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
stevo84
(stevo84)
February 20, 2010, 7:46am
9
No, I did not... and it sure works! I knew that it should be easier
Thank You for another piece of knowledge, Rob and Xavier