Something I've noticed. Is it possible to use gsub to sub in quotations?
text = text.gsub(/'/, """)Obviously doesn't work.
Does the first part work?
Also, for the second (quoted) part, the problem is that you're not escaping the quote you want to insert into the text. Ruby is interpreting that as
text = text.gsub(/'/, """)#set 'text' to 'text' after all instances of ' have been replaced with <invalid string>Ruby sees the first two "s and thinks 'ok, an empty string, which is fine. The third one comes in, however, and opens a new string. This one is never closed, so you will get many a error with invalid syntax, and things just not functioning as you would expect. To escape the quote, simpley replace
""" with
"\""The backlash is the escape character in nearly all programming languages. It's used to literally input special characters into a string, such as a quote, which would normally mark the beginning or end of a string.
Notes
With
text = text.gsub(/'/, "\"")you can simplify this with the gsub
! method. When a method has a '!' at the end of a name in Ruby, it will directly modify the object that calls the method. For example,
text = text.gsub(/'/, "\"")would be equivalent, and probably more efficient than
text.gsub!(/'/, "\"") Additionally, if you put a double quote inside a set of single quotes, Ruby will understand to input a double quote. For example,
text = "Apple"puts "The value of text is '#{text}'"would output
Code:
The value of text is 'Apple'