Title / Description
Code ## See terrible article here http://caiustheory.com/ruby-shortcuts ## 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. %{} # => "" %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. %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. %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. %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. %s{foo} # => :foo %s{foo/bar} # => :"foo/bar" :"foo-#{5}" # => :"foo-5" %s{foo-#{5}} # => :"foo-\#{5}" ## 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. `echo hi` # => "hi\n" %x{echo hi} # => "hi\n" %x{echo #{5}} # => "5\n" # Now see if /.../ Regexp syntax and / operator play well together... 5.6 / 7 # end a / 7 # end 5.6 / a # end a / b # end a / 2.35 # end METHOD_NAME = / #{IDENT} [?!]? /xo METHOD_NAME_EX = / #{METHOD_NAME} # common methods: split, foo=, empty?, gsub! | \*\*? # multiplication and power | [-+~]@? # plus, minus | [\/%&|^`] # division, modulo or format strings, &and, |or, ^xor, `system` | \[\]=? # array getter and setter | <=?>? | >=? # comparison, rocket operator | << | >> # append or shift left, shift right | ===? # simple equality and case equality /ox
Author
Highlight as C C++ CSS Clojure Delphi ERb Groovy (beta) HAML HTML JSON Java JavaScript PHP Plain text Python Ruby SQL XML YAML diff code