$30 off During Our Annual Pro Sale. View Details »

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. stupid ruby tricks
    and some useful ones
    Nick Quaranto @qrush

    View Slide

  2. stupid ruby tricks
    and some useful ones
    Nick Quaranto @qrush
    1/23 - 5%

    View Slide

  3. 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%

    View Slide

  4. 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%

    View Slide

  5. 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%

    View Slide

  6. underscores in numbers
    1_000.times do
    puts "ROFL!"
    end
    (1..300_000_000).each do |n|
    puts "LMAO!"
    end
    5/23 - 22%

    View Slide

  7. underscore in irb
    # %w(a b c).map(&:succ)
    ["b", "c", "d"]
    # _
    ["b", "c", "d"]
    # letters = _
    ["b", "c", "d"]
    6/23 - 27%

    View Slide

  8. string concatenation
    # "ab" + "cd"
    "abcd"
    # "ab" "cd"
    "abcd"
    7/23 - 31%

    View Slide

  9. string interpolation, normal
    a = 2
    b = "1#{a}3"
    p b
    123
    8/23 - 35%

    View Slide

  10. string interpolation, also normal
    @a = 5
    b = "4#{@a}6"
    p b
    456
    9/23 - 40%

    View Slide

  11. 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%

    View Slide

  12. interpolate into symbols
    p :this_is_a_symbol.object_id
    klass = "symbol"
    p :"this_is_a_#{klass}".object_id
    333788
    333788
    11/23 - 48%

    View Slide

  13. string delimiters
    "String!"
    'String!'
    %{String!}
    %|String!|
    %`String!`
    %;String!;
    12/23 - 53%

    View Slide

  14. String#*
    # "*" * 80
    ******************************************
    13/23 - 57%

    View Slide

  15. 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%

    View Slide

  16. redo
    # s = ''
    ""
    # 1.upto(5) { |i| s << i.to_s; redo if s.size < 3 }
    1
    # s
    "1112345"
    15/23 - 66%

    View Slide

  17. pulling elements out of arrays
    # args = [1, 2, 3]
    # first, *rest = args
    # first
    1
    # rest
    [2, 3]
    16/23 - 70%

    View Slide

  18. 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%

    View Slide

  19. 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%

    View Slide

  20. 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%

    View Slide

  21. 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%

    View Slide

  22. 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%

    View Slide

  23. 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%

    View Slide