1 - Extract regular expression matches quickly
x = 'this is a test'
x[/[aeiou].+?[aeiou]/] # => 'is i'
2 - Shortcut for Array#join
%w{this is a test} * ", " # => "this, is, a, test"
h = { :name => "Fred", :age => 77 }
h.map { |i| i "=" } "n" # => "age=77nname=Fred"
3 - Format decimal amounts quickly
money = 9.5
"%.2f" % money # => "9.50"
4 - Interpolate text quickly
"[%s]" % "same old drag" # => "[same old drag]"
You can use an array of elements to substitute in too:
x = %w{p hello p}
"<%s>%s</%s>" % x # => "<p>hello</p>"
5 - Delete trees of files
require 'fileutils'
FileUtils.rm_r 'somedir'
6 - Exploding enumerables
a = %w{a b}
b = %w{c d}
[a + b] # => [["a", "b", "c", "d"]]
[*a + b] # => ["a", "b", "c", "d"]
a = { :name => "Fred", :age => 93 }
[a] # => [{:name => "Fred", :age =>93}]
[*a] # => [[:name, "Fred"], [:age, 93]]
a = %w{a b c d e f g h}
b = [0, 5, 6]
a.values_at(*b).inspect # => ["a", "f", "g"]
7 - Cut down on local variable definitions
(z ||= []) << 'test'
8 - Using non-strings or symbols as hash keys
does = is = { true => 'Yes', false => 'No' }
does[10 == 50] # => "No"
is[10 > 5] # => "Yes"
9 - Use 'and' and 'or' to group operations for single liners
queue = []
%w{hello x world}.each do |word|
queue << word and puts "Added to queue" unless word.length < 2
end
puts queue.inspect
10 - Do something only if the code is being implicitly run, not required
if FILE == $0
# Do something.. run tests, call a method, etc. We're direct.
end
11 - Quick mass assignments
def my_method(*args)
a, b, c, d = args
end
def initialize(args)
args.keys.each { |name| instance_variable_set "@" + name.to_s, args[name] }
end
12 - Use ranges instead of complex comparisons for numbers
year = 1972
puts case year
when 1970..1979: "Seventies"
when 1980..1989: "Eighties"
when 1990..1999: "Nineties"
end
13 - Use enumerations to cut down repetitive code
%w{rubygems daemons eventmachine}.each { |x| require x }
14 - The Ternary Operator
qty = 1
qty == 0 ? 'none' : qty == 1 ? 'one' : 'many'
(qty == 0 ? 'none' : (qty == 1 ? 'one' : 'many'))
16 - Fight redundancy with Ruby's "logic" features
def is_odd(x)
x % 2 != 0
end
class String
def contains_digits?
self[/\d/] ? true : false
end
end
17 - See the whole of an exception's backtrace
def do_division_by_zero; 5 / 0; end
begin
do_division_by_zero
rescue => exception
puts exception.backtrace
end
18 - Allow both single items AND arrays to be enumerated against
[*items].each do |item|
end
19 - Rescue blocks don't need to be tied to a 'begin'
def x
begin
rescue
end
end
def x
rescue
end
20 - Block comments
puts "x"
=begin
this is a block comment
You can put anything you like here!
puts "y"
=end
puts "z"
21 - Rescue to the rescue
h = { :age => 10 }
h[:name].downcase
h[:name].downcase rescue "No name"