Skip to content

Instantly share code, notes, and snippets.

irb(main):024:0> Benchmark.measure{1_000_000.times{'hello'}}
=> 0.210000 0.000000 0.210000 ( 0.202565)
irb(main):025:0> Benchmark.measure{1_000_000.times{:hello}}
=> 0.050000 0.000000 0.050000 ( 0.046498)
irb(main):026:0> Benchmark.measure{10_000_000.times{'hello'}}
=> 1.500000 0.000000 1.500000 ( 1.499443)
irb(main):027:0> Benchmark.measure{10_000_000.times{:hello}}
@AJFaraday
AJFaraday / gist:676c6c59c7c922f9d148
Created July 31, 2014 09:22
Why use symbols instead of strings:
irb(main):024:0> Benchmark.measure{1_000_000.times{'hello'}}
=> 0.210000 0.000000 0.210000 ( 0.202565)
irb(main):025:0> Benchmark.measure{1_000_000.times{:hello}}
=> 0.050000 0.000000 0.050000 ( 0.046498)
irb(main):026:0> Benchmark.measure{10_000_000.times{'hello'}}
=> 1.500000 0.000000 1.500000 ( 1.499443)
irb(main):027:0> Benchmark.measure{10_000_000.times{:hello}}
# gsub! doesn't return anything when no changes are made
irb(main):006:0> 'abc'.gsub('c','d')
=> "abd"
irb(main):007:0> 'abc'.gsub!('c','d')
=> "abd"
irb(main):008:0> 'abc'.gsub!('d','c')
=> nil
# It's usual usage pattern (working on an existing object) still works
@AJFaraday
AJFaraday / gist:907ded93cade7d57988e
Created September 12, 2014 15:39
Just discovered this line in my cron tab...
# m h dom mon dow command
41 9 19 12 * cd /home/ajfaraday/NetworkEnsemble/ && ruby script/play.rb sequences/christmas/flashmob.csv
days = [
'first','second','third','fourth','fifth','sixth',
'seventh','eighth','nineth','tenth','eleventh','twelveth'
]
gifts = [
'a partridge in a pear tree', 'two turtle doves and','three french hens',
'four calling birds', 'five gold rings', 'six geese a laying',
'seven swans a swimming', 'eight maids a milking', 'nine ladies dancing',
'ten lords a leaping', 'eleven pipers piping', 'twelve drummers drumming'
class Santa < Elf
ATTRIBUTES = ['Jolly','Old']
has_many :children
def christmas
list = children.where(:behaviour => 'nice')
2.times do
list.reject!{|x| x.naughty? }
Line 3:
9: comma
18: comma
29: comma
Line 4:
10: comma
31: comma
Line 7:
2: letter i missing a dot
9: open square brackets ( [ )
TOLERANCE = 50
def near_top_wall?
sensors.position.y >= (arena.height - TOLERANCE)
end
def near_bottom_wall?
sensors.position.y <= TOLERANCE
end
@AJFaraday
AJFaraday / gist:6b11a6f350de2b3c9297
Created July 15, 2015 12:39
migrations on inconsistent databases (work-around)
module ActiveRecord
class Migration
def self.rename_table_if_needed(old_name, new_name)
rename_table(old_name, new_name)
rescue => ex
puts "rename_table(#{old_name},#{new_name}) failed #{ex.message}"
end
@AJFaraday
AJFaraday / _form.html.erb
Created July 29, 2015 09:20
Indentation oddity in Rubymine (JS embedded in ERB)
<!-- Some form or other -->
<script type='application/javascript'>
// Expected indentation
<% if @validate %>
run_validation();
<% end %>
// rubymine standard indentation