Skip to content

Instantly share code, notes, and snippets.

@beakr
beakr / gist:2384234
Created April 14, 2012 12:52
Packing something into an array from an argument.
def drink(drink)
@drinks << drink
end
def initialize
@drinks = []
end
@beakr
beakr / gist:2384254
Created April 14, 2012 12:55
Defining methods without 'def'.
def hello; "Hello"; end
define_method(hello) { "Hello" }
@beakr
beakr / gist:2384282
Created April 14, 2012 13:01
Ruby transpose.
a = [:a, :b, :c]
b = [1, 2, 3]
[a, b].transpose #=> [[:a, :b, :c], [1, 2, 3]]
@beakr
beakr / gist:2384694
Created April 14, 2012 14:18
Sample DSL.
class Drinks
def initialize(&block) # Initialize drinks
@drinks = []
instance_eval &block
end
def drink(d)
@drinks << d
end # Add drink
class Drinks < Array
def initialize(&block)
instance_eval &block
end
# Add drink to array
def drink(d)
self << d
end
@beakr
beakr / gist:2389182
Created April 15, 2012 01:21
Module inherit from another module
module Hello
module World
def hello_world
"Hello world!"
end
end
include World
end
@beakr
beakr / load.rb
Created April 22, 2012 01:28
Load a task from a Ruby function
def my_task
load "tasks/my_task.rb"
end
@beakr
beakr / codemirror.js.erb
Created April 25, 2012 23:32
Embed Rails server info into Javascript
var editor = CodeMirror.fromTextArea(getElementById('editor'), {
theme: <%= @user.theme %> <%# Get users favorite theme from database. %>
});
@beakr
beakr / gist:2520307
Created April 28, 2012 17:12
Pass in an array for common fileutils (touch, mkdir, etc.) to evaluate through.
require 'fileutils'
touch %w( file1 file2 file3 )
@beakr
beakr / gist:2551994
Created April 29, 2012 17:11
Require error
# lib/socks.rb
require 'socks/version'
require 'socks/base_controller'
require 'socks/content_helpers'
# lib/socks/content_helpers.rb
module Socks