Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Possible Duplicate:
Which style of Ruby string quoting do you favour?

Is there a good rule of thumb about when to use single or double quotes in ruby, and especially rails apps?

Doesn't one use more memory than the other? Is there any sort of convention the rails community has settled on?

like image 355
Lee McAlilly Avatar asked Feb 11 '11 04:02

Lee McAlilly


People also ask

Should I use single or double quotes programming?

In JavaScript, single (' ') and double (“ ”) quotes are frequently used for creating a string literal. Generally, there is no difference between using double or single quotes, as both of them represent a string in the end.

What is the main difference between single quotes and double quotes in Ruby?

The basic difference between these two methods is Single quotes can not hold/print escape sequences directly, while double quotes can. i.e. Double quoted strings are used for String Interpolation in Ruby.

What is the main difference between single quotes and double quotes?

General Usage Rules In America, Canada, Australia and New Zealand, the general rule is that double quotes are used to denote direct speech. Single quotes are used to enclose a quote within a quote, a quote within a headline, or a title within a quote.

What is the difference between single and double quotes to delineate strings?

The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string. It interprets the Escape sequences. Each variable will be replaced by its value.


1 Answers

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
like image 78
Mahesh Avatar answered Sep 30 '22 21:09

Mahesh