Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Stupid Ruby Tricks

Stupid Ruby Tricks

Learn some useful and sometimes hidden Ruby features.

Given at WNYRuby in December 2011. http://www.meetup.com/Western-New-York-Ruby/

Nick Quaranto

January 12, 2012
Tweet

More Decks by Nick Quaranto

Other Decks in Programming

Transcript

  1. ruby -c: Syntax checking $ echo "class Foo" | ruby

    -c -:1: syntax error, unexpected $end $ echo "class Foo; end" | ruby -c Syntax OK 2/23 - 9%
  2. ruby -w: Indent checking (1.9+) $ cat bad.rb class Foo

    def bar if false puts "LOL" end end $ ruby -w bad.rb bad.rb:5: warning: mismatched indentations at 'end' with 'if' bad.rb:6: warning: mismatched indentations at 'end' with 'def bad.rb:6: syntax error, unexpected $end, expecting keyword_en 3/23 - 14%
  3. ruby -p, -e, -i $ echo matz | ruby -p

    -e '$_.tr! "a-z", "A-Z"' MATZ $ echo matz > /tmp/junk $ cat /tmp/junk matz $ ruby -p -i.bak -e '$_.upcase!' /tmp/junk $ cat /tmp/junk MATZ $ cat /tmp/junk.bak matz 4/23 - 18%
  4. underscore in irb # %w(a b c).map(&:succ) ["b", "c", "d"]

    # _ ["b", "c", "d"] # letters = _ ["b", "c", "d"] 6/23 - 27%
  5. string interpolation, WTF? @a = 8 b = "#@a" c

    = "7#@a" d = "7#@a9" p b p c p d 8 78 7 10/23 - 44%
  6. DATA and __END__ $ cat data.rb p YAML.load(DATA.read)[ARGV.first] __END__ foo:

    "Bar" baz: "LOL!" $ ruby -ryaml data.rb nil $ ruby -ryaml data.rb foo "Bar" $ ruby -ryaml data.rb bar "LOL!" 14/23 - 61%
  7. redo # s = '' "" # 1.upto(5) { |i|

    s << i.to_s; redo if s.size < 3 } 1 # s "1112345" 15/23 - 66%
  8. pulling elements out of arrays # args = [1, 2,

    3] # first, *rest = args # first 1 # rest [2, 3] 16/23 - 70%
  9. splat arguments, anywhere (1.9+) # def foo(*args); p args; end

    # foo(1, 2, 3, 4) [1, 2, 3, 4] # def bar(first, *args); p first; p args; end # bar(1, 2, 3, 4) 1 [2, 3, 4] # def baz(first, *args, last); p first; p args; p last; end # baz(1, 2, 3, 4) 1 [2, 3] 4 17/23 - 74%
  10. destructuring arrays def touch_down yield [3, 7] puts "touchdown!" end

    touch_down do |(first_down, second_down)| puts "#{first_down} yards on the run" puts "#{second_down} yards passed" end 3 yards on the run 7 yards passed touchdown! 18/23 - 79%
  11. Enumerable#any? / #all? # [false, false, true].any? true # [false,

    false, nil].any? false # [true, true, true].all? true # [true, true, false].all? false 19/23 - 83%
  12. Hash#new with a block # smash = Hash.new { |hash,

    key| hash[key] = "a #{key} just got SMASHED!" } {} # smash[:plum] = "cannot smash." {:plum=>"cannot smash."} # smash[:watermelon] {:plum=>"cannot smash.", :watermelon=>"a watermelon just got SMASHED!"} 20/23 - 87%
  13. Hash::[] # Hash[:apples, 3, :bananas, 2] {:apples=>3, :bananas=>2} # Hash[[:apples,

    3, :bananas, 2] * 300_000] SystemStackError: stack level too deep Ruby bug warning! 21/23 - 92%
  14. j and jj (1.9+) # require 'json' # h =

    {a: 1, b:2} {:a=>1, :b=>2} # j(h) {"a":1,"b":2} # jj(h) { "a": 1, "b": 2 } 22/23 - 96%