Slide 1

Slide 1 text

stupid ruby tricks and some useful ones Nick Quaranto @qrush

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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%

Slide 4

Slide 4 text

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%

Slide 5

Slide 5 text

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%

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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%

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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%

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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%

Slide 19

Slide 19 text

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%

Slide 20

Slide 20 text

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%

Slide 21

Slide 21 text

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%

Slide 22

Slide 22 text

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%

Slide 23

Slide 23 text

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%