Skip to content

Instantly share code, notes, and snippets.

@ipmsteven
ipmsteven / unbind.rb
Created January 20, 2015 21:11
private unbind
class A
private
def haha
puts "haha"
end
end
A.new.method(:haha).unbind.bind(Class.new(A).new).call
# "haha"
@ipmsteven
ipmsteven / a_m_c_and_prepend.rb
Last active August 29, 2015 14:11
alias_method_chain and prepend
# 1. alias_method before prepend
module PreA
def foo
puts "pre a"
super
end
end
class A
def self.foo
@ipmsteven
ipmsteven / autoload_patch.rb
Created November 1, 2014 00:10
print autoload object name
module ModulePrepend
def autoload(*args)
puts args.inspect
super
end
end
class Module
prepend ModulePrepend
end
@ipmsteven
ipmsteven / ruby_object_model.rb
Created October 29, 2014 07:45
ruby object model
#!/usr/bin/ruby
COLON = ':'.freeze
UNDERSCORE = '_'.freeze
TAB = "\t".freeze
NAMESPACE = 'NAMESPACE_YOU_WANT_MODELING::'.freeze
# Colons are invalid characters in DOT nodes.
# Replace them with underscores.
# http://www.graphviz.org/doc/info/lang.html
@ipmsteven
ipmsteven / multiple_initializers.rb
Last active November 13, 2015 08:41
Ruby MultipleInitializers
module MultipleInitializers
def new_using(method, *args)
instance = self.allocate
raise 'implement me' unless instance.respond_to?(method.to_sym, true)
instance.send(method.to_sym, *args)
instance
end
end
class A
def initialize
haha
end
def haha
puts "a"
end
end
@ipmsteven
ipmsteven / gist:5312672181493315910d
Created September 23, 2014 17:09
undef in ruby
class SimpleProxy
instance_methods.each do |m|
unless m.to_s =~ /^(?:nil\?|send|object_id|to_a|tap)$|^__|^respond_to|instance_variable_get/
undef_method m
end
end
def initialize(object)
@object = object
@ipmsteven
ipmsteven / gist:099b8a4b0b0d90fd4446
Created September 4, 2014 08:03
another ruby regular expression based solution for format_currency
def format_currency(number)
return number if number.blank?
("%.2f" % number).reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse
end
@ipmsteven
ipmsteven / gist:1ee106da07e1821cedbe
Created September 4, 2014 07:56
simple rails number_to_currency
def format_currency(number)
return number if number.blank?
parts = ("%.2f" % number).split('.')
parts[0].gsub!(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
parts.join('.')
end
/**
* impress.js
*
* impress.js is a presentation tool based on the power of CSS3 transforms and transitions
* in modern browsers and inspired by the idea behind prezi.com.
*
*
* Copyright 2011-2012 Bartek Szopka (@bartaz)
*
* Released under the MIT and GPL Licenses.