Learn some useful and sometimes hidden Ruby features.
Given at WNYRuby in December 2011. http://www.meetup.com/Western-New-York-Ruby/
stupid ruby tricksand some useful onesNick Quaranto @qrush
View Slide
stupid ruby tricksand some useful onesNick Quaranto @qrush1/23 - 5%
ruby -c: Syntax checking$ echo "class Foo" | ruby -c-:1: syntax error, unexpected $end$ echo "class Foo; end" | ruby -cSyntax OK2/23 - 9%
ruby -w: Indent checking (1.9+)$ cat bad.rbclass Foodef barif falseputs "LOL"endend$ ruby -w bad.rbbad.rb:5: warning: mismatched indentations at 'end' with 'if'bad.rb:6: warning: mismatched indentations at 'end' with 'defbad.rb:6: syntax error, unexpected $end, expecting keyword_en3/23 - 14%
ruby -p, -e, -i$ echo matz | ruby -p -e '$_.tr! "a-z", "A-Z"'MATZ$ echo matz > /tmp/junk$ cat /tmp/junkmatz$ ruby -p -i.bak -e '$_.upcase!' /tmp/junk$ cat /tmp/junkMATZ$ cat /tmp/junk.bakmatz4/23 - 18%
underscores in numbers1_000.times doputs "ROFL!"end(1..300_000_000).each do |n|puts "LMAO!"end5/23 - 22%
underscore in irb# %w(a b c).map(&:succ)["b", "c", "d"]# _["b", "c", "d"]# letters = _["b", "c", "d"]6/23 - 27%
string concatenation# "ab" + "cd""abcd"# "ab" "cd""abcd"7/23 - 31%
string interpolation, normala = 2b = "1#{a}3"p b1238/23 - 35%
string interpolation, also normal@a = 5b = "4#{@a}6"p b4569/23 - 40%
string interpolation, WTF?@a = 8b = "#@a"c = "7#@a"d = "7#@a9"p bp cp d878710/23 - 44%
interpolate into symbolsp :this_is_a_symbol.object_idklass = "symbol"p :"this_is_a_#{klass}".object_id33378833378811/23 - 48%
string delimiters"String!"'String!'%{String!}%|String!|%`String!`%;String!;12/23 - 53%
String#*# "*" * 80******************************************13/23 - 57%
DATA and __END__$ cat data.rbp YAML.load(DATA.read)[ARGV.first]__END__foo: "Bar"baz: "LOL!"$ ruby -ryaml data.rbnil$ ruby -ryaml data.rb foo"Bar"$ ruby -ryaml data.rb bar"LOL!"14/23 - 61%
redo# s = ''""# 1.upto(5) { |i| s << i.to_s; redo if s.size < 3 }1# s"1112345"15/23 - 66%
pulling elements out of arrays# args = [1, 2, 3]# first, *rest = args# first1# rest[2, 3]16/23 - 70%
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]417/23 - 74%
destructuring arraysdef touch_downyield [3, 7]puts "touchdown!"endtouch_down do |(first_down, second_down)|puts "#{first_down} yards on the run"puts "#{second_down} yards passed"end3 yards on the run7 yards passedtouchdown!18/23 - 79%
Enumerable#any? / #all?# [false, false, true].any?true# [false, false, nil].any?false# [true, true, true].all?true# [true, true, false].all?false19/23 - 83%
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%
Hash::[]# Hash[:apples, 3, :bananas, 2]{:apples=>3, :bananas=>2}# Hash[[:apples, 3, :bananas, 2] * 300_000]SystemStackError: stack level too deepRuby bug warning!21/23 - 92%
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%