Ruby code to change external quotes in strings?

Does anyone know of a Ruby stone (or native or native syntax, for that matter) that works with external string quotation marks?

I write methods over and over again:

remove_outer_quotes_if_quoted( myString, chars ) -> aString add_outer_quotes_unless_quoted( myString, char ) -> aString 

The first tests are myString to see if its start and end characters match the same character in chars . If so, it returns a string with quotes removed. Otherwise, it returns it unchanged. chars by default, is assigned a list of quotation mark characters.

The second test is myString to find out if it starts and ends with char . If so, it returns the string unchanged. If not, it returns a string with char , attached before and after, and any implemented char event is executed with a backslash. char defaults to the first character in the default character list.

(My manual methods do not have such detailed names, of course.)

I looked at similar methods in public repositories, but cannot find anything like it. Am I the only one who has to do this? If not, how does everyone do it?

+4
source share
3 answers

If you do this a lot, you can add a method to String:

 class String def strip_quotes gsub(/\A['"]+|['"]+\Z/, "") end end 

Then you can just call string.strip_quotes .

Adding quotes is similar:

 class String def add_quotes %Q/"#{strip_quotes}"/ end end 

This is called string.add_quotes and uses strip_quotes before adding double quotes.

+4
source

It could be splain, how to remove and add them:

 str1 = %["We're not in Kansas anymore."] str2 = %['He said, "Time flies like an arrow, Fruit flies like a banana."'] puts str1 puts str2 puts puts str1.sub(/\A['"]/, '').sub(/['"]\z/, '') puts str2.sub(/\A['"]/, '').sub(/['"]\z/, '') puts str3 = "foo" str4 = 'bar' [str1, str2, str3, str4].each do |str| puts (str[/\A['"]/] && str[/['"]\z/]) ? str : %Q{"#{str}"} end 

The original two lines:

 # >> "We're not in Kansas anymore." # >> 'He said, "Time flies like an arrow, Fruit flies like a banana."' 

Final quotation marks:

 # >> We're not in Kansas anymore. # >> He said, "Time flies like an arrow, Fruit flies like a banana." 

Adding quotes if necessary:

 # >> "We're not in Kansas anymore." # >> 'He said, "Time flies like an arrow, Fruit flies like a banana."' # >> "foo" # >> "bar" 
+2
source

I would use value = value[1...-1] if value[0] == value[-1] && %w[' "].include?(value[0]) . In short, this simple code checks whether the first and last char lines match, and deletes them if they are single / double quotes.Also, as many types of quotes as necessary can be added.

 %w["adadasd" 'asdasdasd' 'asdasdasd"].each do |value| puts 'Original value: ' + value value = value[1...-1] if value[0] == value[-1] && %w[' "].include?(value[0]) puts 'Processed value: ' + value end 

In the above example, the following will be printed:

 Original value: "adadasd" Processed value: adadasd Original value: 'asdasdasd' Processed value: asdasdasd Original value: 'asdasdasd" Processed value: 'asdasdasd" 
0
source

All Articles