0

I recently asked a question (heres the link) regarding opening a link in a new page. I found there were two answers.

:target => '_blank' 
target: "_blank"

I was hoping someone could explain the difference between single and double quotations and why both of the above work. I understand the second option is only achievable on more recent versions of rails (I'm using 3.2.2).

Community
  • 1
  • 1
glennm
  • 325
  • 1
  • 5
  • 12

2 Answers2

5

basically, this is Ruby's hash, ( for more info, please refer to "@mu is too shot"'s resource )

# always works (works both in Ruby 1.8 and 1.9)
:target => '_blank'    

# works in 1.9 only 
target: "_blank"

both of them can assign hash's elements.

however the code below is incorrect.

# this is incorrect
target: = "_blank"
Siwei
  • 19,858
  • 7
  • 75
  • 95
  • 1
    There's no such thing as "classic" and "1.9" style, there's the style that always works and the new alternative style that only works in limited cases: http://stackoverflow.com/a/10004344/479863 – mu is too short Apr 24 '12 at 05:32
  • 2
    But `=>` still isn't a 1.8 thing, it is perfectly valid and often necessary in 1.9. Yes, I'm on a crusade against the shiny new (and broken) alternative Hash literal syntax :) – mu is too short Apr 24 '12 at 06:03
  • @SiweiShen, thanks for picking up my error in the second example. I've fixed the error so it doesn't become the focus. – glennm Apr 24 '12 at 06:23
  • @muistooshort, I just read your comments on the link you provided. I'm interested to learn more so I'll read up on it. But I liked your comments. Thanks! – glennm Apr 24 '12 at 06:25
-1
1.9.2-p290 :009 > { :name => "soundar" } == { name: "soundar" }
=> true
Soundar Rathinasamy
  • 6,658
  • 6
  • 29
  • 47