Ruby Shortcuts - CaiusTheory

Ask questions Research chat →

https://caiustheory.com/ruby-shortcuts/ · scraped

ruby

Attachments

Scraped Content

— 354 words · 2026-02-14 17:42:10 UTC ·

Excerpt

There’s a few useful shorthand ways to create certain objects in Ruby, a couple of obvious ones are [] to create an Array and {} to create a Hash (Or block/Proc). There’s some not so obvious ones too, for creating strings, regexes and executing shell commands. With all of the examples I’ve used {} as the delimiter characters, but you can use a variety of characters. Personally I tend to use {} unless the string contains them, in which case I’ll use // or @@. My only exception appears to be %w, for which I tend to use (). ### Strings % and %Q are the same as using double quotes, including string interpolation. Really useful when you want to create a string that contains double quotes, but without the hassle of escaping them. ```plain text %{} # => "" %Q{} # => "" %{caius} # => "caius" %{caius #{5}} # => "caius 5" %{some "foo" thing} # => "some \"foo\" thing" ``` %q is equivalent to using single quotes. Behaves exactly the same, no s
There’s a few useful shorthand ways to create certain objects in Ruby, a couple of obvious ones are [] to create an Array and {} to create a Hash (Or block/Proc). There’s some not so obvious ones too, for creating strings, regexes and executing shell commands. With all of the examples I’ve used {} as the delimiter characters, but you can use a variety of characters. Personally I tend to use {} unless the string contains them, in which case I’ll use // or @@. My only exception appears to be %w, for which I tend to use (). ### Strings % and %Q are the same as using double quotes, including string interpolation. Really useful when you want to create a string that contains double quotes, but without the hassle of escaping them. ```plain text %{} # => "" %Q{} # => "" %{caius} # => "caius" %{caius #{5}} # => "caius 5" %{some "foo" thing} # => "some \"foo\" thing" ``` %q is equivalent to using single quotes. Behaves exactly the same, no string interpolation. ```plain text %q{} # => '' %q{caius} # => "caius" %q{caius #{5}} # => "caius \#{5}" ``` ### Arrays %w is the equivalent of using String#split. It takes a string and splits it on whitespace. With the added bonus of being able to escape whitespace too. %W allows string interpolation. ```plain text %w(foo bar sed) # => ["foo", "bar", "sed"] %w(foo\ bar sed) # => ["foo bar", "sed"] %W(foo #{5} bar) # => ["foo", "5", "bar"] ``` ### Regexes %r is just like using // to create a regexp object. Comes in handy when you’re writing a regex containing / as you don’t have to continually escape it. ```plain text %r{foo|bar} # => /foo|bar/ %r{foo/bar} # => /foo\/bar/ ``` ### Symbols %s creates a symbol, just like writing :foo manually. It takes care of escaping the symbol, but unlike :"" it doesn’t allow string interpolation however. ### Shelling out %x is the same as backticks (``), executes the command in a shell and returns the output as a string. And just like backticks it supports string interpolation.

Visibility

Visible to everyone

Reading Status

Related Bookmarks

My Note


Saved!

Annotations

Export as Markdown
+ Annotate selection

Add Annotation