Which Style of Ruby String Quoting Do You Favour

Which style of Ruby string quoting do you favour?

Don't use double quotes if you have to escape them. And don't fall in "single vs double quotes" trap. Ruby has excellent support for arbitrary delimiters for string literals:

Mirror of Site - https://web.archive.org/web/20160310224440/http://rors.org/2008/10/26/dont-escape-in-strings

Original Site -
http://rors.org/2008/10/26/dont-escape-in-strings

What is the use case for Ruby's %q / %Q quoting methods?

They're extraordinarily useful for escaping HTML with JavaScript in it where you've already "run out" of quoting methods:

link = %q[<a href="javascript:method('call')">link</a>]

I've also found them to be very useful when working with multi-line SQL statements:

execute(%Q[
INSERT INTO table_a (column_a)
SELECT value
FROM table_b
WHERE key='value'
])

The advantage there is you don't need to pay attention to the type of quoting used within your query. It will work with either single, double, or both. They're also a lot less fuss than the HEREDOC style method.

Ruby provides other convenience methods like this such as %r which can construct regular expressions. That avoids slash-itis when trying to write one that handles stuff like http:// that would otherwise have to be escaped.

When to use single vs. double quotes in a Rails app

The difference between using double quotes versus single quotes is that double quotes interpret escaped characters and single quotes preserve them.

Here is an example

 puts "i \n love \n ruby"
#i
#love
#ruby

and

 puts 'i \n love \n ruby'
#i \n love \n ruby

Backslashes in single quoted strings vs. double quoted strings

I'd refer you to "Ruby Programming/Strings" for a very concise yet comprehensive overview of the differences.

From the reference:

puts "Betty's pie shop"

puts 'Betty\'s pie shop'

Because "Betty's" contains an apostrophe, which is the same character as the single quote, in the second line we need to use a backslash to escape the apostrophe so that Ruby understands that the apostrophe is in the string literal instead of marking the end of the string literal. The backslash followed by the single quote is called an escape sequence.

Using string .split (and a regular expression) to check for inner quotes

You want to use this regular expression (see on rubular.com):

/"[^"]*"|'[^']*'|[^"'\s]+/

This regex matches the tokens instead of the delimiters, so you'd want to use scan instead of split.

The […] construct is called a character class. [^"] is "anything but the double quote".

There are essentially 3 alternates:

  • "[^"]*" - double quoted token (may include spaces and single quotes)
  • '[^']*' - single quoted token (may include spaces and double quotes)
  • [^"'\s]+ - a token consisting of one or more of anything but quotes and whitespaces

References

  • regular-expressions.info/Character Class

Snippet

Here's a Ruby implementation:

s = %_foobar "your mom"bar'test course''test lesson'asdf_
puts s

puts s.scan(/"[^"]*"|'[^']*'|[^"'\s]+/)

The above prints (as seen on ideone.com):

foobar "your mom"bar'test course''test lesson'asdf
foobar
"your mom"
bar
'test course'
'test lesson'
asdf

See also

  • Which style of Ruby string quoting do you favour?

Use single quote in string inspection

s = 'a"c'.inspect
s[0] = s[-1] = "'"
puts s.gsub("\\\"", "\"") #=> 'a"c'

How to print backslash in single quoted string in ruby

So long as the strings share the same encoding, byte comparison would be elegant solution without the need for additional escaping.

str.bytes == other.bytes

To display a backslash, you simply escape it with a single backslash:

puts '\\'
\

Additionally, depending on your exact usage, could use the <=> operator:

(str <=> other).zero?

EDIT

To expand a little more, there is a difference in what is displayed to you if you just did something like this:

a = '\\'
p a
=> '\\'

This will show itself as two backslashes, but it is still in fact a single character, not two.

a = '\\'
a.bytes
=> [92] # ASCII code for a backslash (single byte)

String#split in Ruby not behaving as expected

Your question was not very clear

split("\n") - if you want to split by lines

split - if you want to split by spaces

and as I can understand, you do not need chomp, because it removes all the "\n"



Related Topics



Leave a reply



Submit