Nick KallenNick Kallen
Stupid Ruby Quoting Tricks
edit Posted by Nick Kallen on Monday February 25, 2008 at 10:25PM

Let's start with something easy.

Here's the mirepoix

bar = "chicken"

You know single quotes and double quotes, right? These are our salt and pepper.

'frickin #{bar}' # => "frickin #{bar}"
"frickin #{bar}" # => "frickin chicken"

Have you seen %%q and %%Q?

%%q(frickin #{bar}) # => "frickin #{bar}"
%%Q(frickin #{bar}) # => "frickin chicken"
%%q{frickin #{bar}} # => "frickin #{bar}"
%%Q{frickin #{bar}} # => "frickin chicken"

You've also seen """ (triple-quotes) right?

"""frickin #{bar}""" # => "fricken chicken"
"""frickin '#{bar}'""" # => "fricken 'chicken'"
"""frickin "#{bar}"""" # => "fricken "

UPDATE: As John points out in the comments, in addition to triple quotes there are also quintuple quotes, septuple quotes, and so forth. In fact, you can invent your own quoting operator for any odd number of quotes. (Just kidding.)

The truth is, singly and doubly-quoted object concatenate simply by juxtaposition!

"frickin " "chicken" # => "fricken chicken"
'frickin ' 'chicken' # => "fricken chicken"
"frickin " bar # => kabooom!
%%q{frickin } %%q{chicken} # => kablooey!
"""" #=> ""
"""frickin chicken" # => "frickin chicken"

Hokay. So that is all child's play. These are the quoting operators for the big boys.

This is why Ruby's Grammar is not Context-Free:

def foo(x)
  x.reverse
end

foo(<<-EOS)
  Your mom
EOS
# => "mom ruoY"

foo(<<-EOS).reverse
  amanaplanacanalpanama
EOS
# => "amanaplanacanalpanama"

Even better:

puts(DATA.read.reverse)

__END__
Your mom
# => "mom ruoY"

You actually have to copy the above code into a ruby file and run it -- it wont work in IRB.

Comments

  1. John Barnette John Barnette on February 25, 2008 at 11:45PM

    Ruby joins adjacent strings in the parser:

    "hello"    "world" # => "helloworld"
    

    Your triple quotes aren't actually an operator. They're an empty string, followed by a string with some characters in it, followed by another empty string. :-)

    ""     "frickin chicken"    ""
    

    The last triple quote example is awesome: what looks like a variable interpolation is actually the beginning of a comment!

    "" "frickin " #{bar}""""

  2. John Barnette John Barnette on February 25, 2008 at 11:46PM

    Ugh, bad indentation on the last example:

    ""    "frickin chicken "    #{bar}""""
    
  3. Nick Kallen Nick Kallen on February 26, 2008 at 02:55AM

    Ruby is neat.

  4. josh josh on February 26, 2008 at 06:06AM

    There's also quoting in the W syntax.

    fu = 'jitsu'
    puts w(kung-#{fu} ju-#{fu} ju\ #{fu}).join(",")
    puts W(kung-#{fu} ju-#{fu} ju\ #{fu}).join(",")
    

    => kung-#{fu},ju-#{fu},ju #{fu}

    => kung-jitsu,ju-jitsu,ju jitsu

    just sayin'

  5. Ajit Ajit on February 26, 2008 at 12:33PM

    I have just gotten the hang of ruby and it's all i code with now. Great tips, thanks. Your codes are quite flawless.

  6. Pat Maddox Pat Maddox on February 27, 2008 at 09:09AM

    You put the "stupid" in stupid ruby quoting tricks

  7. Michael Hartl Michael Hartl on February 27, 2008 at 08:31PM

    You can also use the super-simple %() syntax:

    %(frickin #{bar})  #=> "frickin chicken"
    

Add a Comment (MarkDown available)