Skip to content

Instantly share code, notes, and snippets.

class << self
def with(expression)
yield expression
end
end
# -----------------------------------------------------
def really_long_and_very_complicated_expression
'quite long text ' * 400
require 'rubygems'
require 'hpricot'
require 'open-uri'
class String
def strip_tags
self.gsub(/<[^<>]+>/,'')
end
end
@fronx
fronx / snippet.rb
Created June 30, 2009 13:28
how to overwrite mixed-in methods while still having access to the original implementation via super
module X
def x
"x"
end
module_function :x
def self.included(base)
base.class_eval do
public :x
end
@fronx
fronx / hello_world.rb
Created July 8, 2009 11:34
a wicked hello world program in ruby
class Fixnum
%w(x y).each do |m|
define_method "norm_#{m}" do
(self + 3 * Urf.__send__("max_#{m}")) % Urf.__send__("max_#{m}")
end
end
end
Urf = Class.new{
attr_reader :feeld
∆_∆ = Class.new do
def initialize
puts "oh, ein neuer BH!"
end
end
∆_∆.new
class Object
# nil if false, else self
def only_if(&block)
self if self.instance_eval(&block)
end
end
puts ''.only_if { length > 0 } || 'previously blank'
# => 'previously blank'
#---------------------------------------------------
# pretty fast
1 2 3 4 5 6 7 8 9 1
x x x x x x
. o o . .
o o o o
#---------------------------------------------------
# pretty fast
1 2 3 4 5 6 7 1
@fronx
fronx / snippet.rb
Created September 23, 2009 12:35
a simple logger
class << (Logger = [])
def to_s
join("\n")
end
def <=(s)
self << line << s << line
end
def line(n = 50)
@fronx
fronx / base64decode.sh
Created October 16, 2009 08:56
base64 decoding for command line
ruby -e "require 'base64'; puts Base64.decode64(ARGV[0])" <somebase64string>
@fronx
fronx / hash_minus.rb
Created October 28, 2009 20:14
difference between two hashes
class Hash
def -(h)
reject do |k, v|
h.has_key?(k) && (v == h[k])
end
end
end
describe Hash, 'difference between two hashes' do
it "should return a hash" do