Skip to content

Instantly share code, notes, and snippets.

@pointcom
Last active January 13, 2018 09:51
Show Gist options
  • Save pointcom/f2ea137ac048e23c8eb742453e64c20f to your computer and use it in GitHub Desktop.
Save pointcom/f2ea137ac048e23c8eb742453e64c20f to your computer and use it in GitHub Desktop.
name = "Stuart"
# To build a string and escape double quote at the same time
%q(Hello, my name is "#{name}")
=> "Hello, my name is \"\#{name}\""
# The same exist for single quote
%Q(Hello, my name is "#{name}")
=> "Hello, my name is \"Stuart\""
# To build an array of strings without interpolation
%w(bobby brown #{name})
=> ["bobby", "brown", "\#{name}"]
# To build an array of strings with interpolation
%W(bobby brown #{name})
=> ["bobby", "brown", "Stuart"]
# Create an array of symbols without interpolation
%i(hello world #{name})
=> [:hello, :world, :"\#{name}"]
# Create an array of symbols with interpolation
%I(hello world #{name})
=> [:hello, :world, :Stuart]
# Execute a system command
%x(ls)
=> "bin\nboot\ndev\netc\nhome\nlib\nlib64\nmedia\nmnt\nopt\nproc\nroot\nrun\nsbin\nsrv\nsys\ntmp\nusr\nvar\n"
# Create a symbol
%s(hello world)
=> :"hello world"
# Create a Regular Expression
%r(^hel\D+d$)
=> /^hel\D+d$/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment