I am learning Ruby. My background is C++/Java/C#. Overall, I like the language, but I am a little confused about why there are so many different ways to accomplish the same thing, each with their own slightly different semantics.
Take string creation, for example. I can use '', "", q%, Q%, or just % to create strings. Some forms support interpolation. Other forms allow me to specify the string delimiters.
Why are there five ways to create string literals? Why would I ever use non-interpolated strings? What advantage does the % syntax have over quoted literals?
I know there must be value in the redundency in Ruby, but my untrained eyes are not clearly seeing it. Please enlighten me.
When you don't want the interpolation, of course. For example, perhaps you're outputting some documentation about string interpolation:
It lets you write strings more naturally, without the quotes, or when you don't want to escape a lot of things, analogous to C#'s string-literal prefix
@.There are many other %-notations:
There's also
%s(for symbols) and some others.This isn't terribly unusual. Consider C#, for example, which has several different ways to generate strings:
new String();"";@"";StringBuilder.ToString(), et cetera.