caius test
Ruby
code posted
by
kib2
created at 23 Apr 15:50
Edit
|
Back
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
## 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 |
2.96 KB in 5 ms with coderay