Skip to content

Instantly share code, notes, and snippets.

class String
@@all = []
def /(other)
@@all << "#{self} #{other}"
@@all.join(' and ')
end
def self.reset
@@all = []
end
class Integer
def to_binary_array(size)
result = to_s(2).split(//).map{ |i| 1 if i == '1' }
result = [nil] + result while result.size < size
result
end
end
class Array
def map_with_other(other)
class Hash
def nested_merge(other)
result = self
other.each do |key, value|
if Hash === self[key] && Hash === other[key]
result[key] = self[key].nested_merge(other[key])
else
result[key] = other[key]
end
end
@fronx
fronx / url_dsl.rb
Created December 14, 2009 22:31 — forked from defunkt/url_dsl.rb
require 'open-uri'
# url dsl -- the ultimate url dsl!
#
# You just can't beat this:
#
# $ irb -r url_dsl
# >> include URLDSL
# => Object
# >> http://github.com/defunkt.json
# Reincarnation for classes
class Class
def reincarnate
buried = Object.__send__(:remove_const, self.name)
Object.const_set(self.name, Class.new(buried))
end
end
class Abc
require 'rubygems'
require 'reincarnation'
module A
def foo
"foo"
end
end
module B
@fronx
fronx / gist:281436
Created January 19, 2010 23:40 — forked from dhh/gist:281420
class Notifier < ActionMailer::Base
delivers_from '[email protected]'
def welcome(user)
@user = user # available to the view
mail(:subject => 'Welcome!', :to => user.email_address)
# auto renders both welcome.text.erb and welcome.html.erb
end
def goodbye(user)
@fronx
fronx / README.txt
Created March 15, 2010 21:33 — forked from rmm5t/README.md
You can represent time statements in most western languages where
a prefix and/or suffix is used.
The default case is to use suffix only (as in English), which you
do by providing the `suffixAgo` and `suffixFromNow` settings in
the strings hash (earlier versions of timeago used the deprecated
`ago` and `fromNow` options). If present, they are used.
2 minutes [suffixAgo]
2 minutes [suffixFromNow]
@fronx
fronx / omg.rb
Created March 16, 2010 06:32 — forked from wycats/omg.rb
require ".bundle/environment"
Bundler.setup
require "action_controller/railtie"
class FooController < ActionController::Base
def bar
self.response_body = "HELLO"
end
end
@fronx
fronx / find_tweets.rb
Created April 20, 2010 19:18
search in twitter json records
#! /usr/bin/env ruby
require 'rubygems'
require 'json'
@user, @q = ARGV[0], ARGV[1]
puts "searching '#{@user}' for '#{@q}'"
def tweets
result = []
(1..50).to_a.each do |page|