Skip to content

Instantly share code, notes, and snippets.

require 'socks/templates'
module Myapp
class App
include Socks::Templates
Router = HttpRouter.new do
# ...
end
end
end
@beakr
beakr / gist:2586923
Created May 3, 2012 16:17
RSpec helpers
RSpec::configure do
def some_helper
puts "Do something that helps RSpec here."
end
end
load ('Somefile') || ('somefile') # Load a custom file
@beakr
beakr / sub-do-attr.rb
Created May 7, 2012 02:39
Nested do block attributes with instance_eval
class Town
def initialize(&block)
@attr = some_attr
instance_eval &block
end
def restaurant(name, &block)
@nested_attr = "define attributes affecting restaurant here."
instance_eval &block
end
@beakr
beakr / gist:2636953
Created May 8, 2012 16:25
Pash in hash keys to string.
puts "This is %{a_string}" %{ :a_string => 'a string.' }
@beakr
beakr / gist:2663143
Created May 11, 2012 23:44
Coffeescript -- safe extension of native objects.
Object.defineProperty String, 'capitalize', value: ->
this[0].toUpperCase() + this[1...]
console.log char for char of 'str'
# => 0, 1, 2
@beakr
beakr / version.rb
Created May 12, 2012 22:02
Join version numbers with '.'
module Socks
module VERSION
MAJOR = '0'
MILD = '2'
MINOR = '10'
PRE = 'beta'
FULL = [MAJOR, MILD, MINOR, PRE].compact.join('.') # 0.2.10.beta
end
end
@beakr
beakr / gist:2669372
Created May 12, 2012 22:09
Why I like system() more than Rake's sh()
system("echo 'Foo!'")
#=> Foo!
require 'rake'
sh("echo 'Foo!'")
#=>
# echo "Foo!" # It prints the commnd it's about to do!
# Foo! # Then runs it.
@beakr
beakr / gist:2669518
Created May 12, 2012 22:36
Ruby compact().
# It's good to remove any nil values, thus cleaning up an array when in use.
[1, 2, nil 3, nil].compact
#=> [1, 2, 3]
@beakr
beakr / sh.rb
Created May 12, 2012 22:43
Function that uses system() for shell commands
# Rake's sh() can have a negative effect by printing the given
# command before running it. (see https://gist.github.com/2669372)
# This can look pretty ugly. Luckily, using system(), you can write tyour own method to avoid this.
def sh(code)
system(code) # Simple.
end