[ ruby ] %i( ) notation

To help reduce escape character.

Example

=> %{Hello World}
=> %<Hello World>
=> %(Hello World)
=> %[Hello World]
=> %?Hello World?
=> %?Hello?World? #This fails
=> %?Hello\?World? #This is fine because the inner ? is escaped
=> %<Hello < World> #This will break because the internal brackets arent' balanced
=> %{Hello {there} World} #This works just fine!

Decorating the %

q string
Q interpolated string
w array of strings
W interpolated array of strings

=> num = 2
=> %q{1+1=#{num}} #evaluates to "1+1=\#{num}"
=> %Q{1+1=#{num}} #evaluates to "1+1=2"
=> %w{This is an array} #evaluates to ["this", "is", "an", "array"]

%r[ ] # Interpolated Regexp (flags can appear after the closing delimiter)
%i[ ] # Non-interpolated Array of symbols, separated by whitespace
%I[ ] # Interpolated Array of symbols, separated by whitespace

%x[ ] # Interpolated shell command

%i{foo bar} # => [:foo, :bar]

ref : https://mikeyhogarth.wordpress.com/2011/11/24/notation-for-ruby-literals/,

http://stackoverflow.com/questions/8816877/is-there-a-literal-notation-for-an-array-of-symbols

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s