Skip to content

Instantly share code, notes, and snippets.

View sikachu's full-sized avatar

Prem Sichanugrist sikachu

View GitHub Profile
@sikachu
sikachu / gist:4982127
Created February 19, 2013 00:54
calling object's method in method definition in Ruby
irb(main):001:0> class Foo
irb(main):002:1> def initialize(a, b = a.to_s)
irb(main):003:2> puts [a, b].inspect
irb(main):004:2> end
irb(main):005:1> end
=> nil
irb(main):006:0> Foo.new 1
[1, "1"]
=> #<Foo:0x007f973d9ca410>
@sikachu
sikachu / 01_function.zsh
Last active December 13, 2015 21:58
Open up the browser and create a new pull request
gpr() {
current_branch=`git rev-parse --abbrev-ref HEAD`
if [[ $1 != '' ]]; then
upstream=$1
else
upstream='master'
fi
origin_username=`git remote -v | grep 'origin.\+fetch' | sed "s/^.*github.com[\/:]\(.*\)\/\(.*\)\.git.*$/\1/"`
origin_repository=`git remote -v | grep 'origin.\+fetch' | sed "s/^.*github.com[\/:]\(.*\)\/\(.*\)\.git.*$/\2/"`
if [[ `git remote | grep sikachu` != '' ]]; then
Benchmark.bmbm do |x|
x.report('start_with?') { 10_000.times { "fooo".start_with?('foo') }}
x.report('regexp') { 10_000.times { "fooo" =~ /^foo/ }}
end
class User
has_many :topics
has_many :posts, through: topics
end
class Topic
belongs_to :user
has_many :posts
end
@sikachu
sikachu / gist:3057373
Created July 6, 2012 00:53
window.it from Jasmine monkeypatched to show error if test is not implemented.
window.it = (desc, func) ->
if func?
jasmine.getEnv().it(desc, func)
else
jasmine.getEnv().it desc, ->
throw "test is undefined"
@sikachu
sikachu / down_wwdc_2012.rb
Created June 11, 2012 20:47
Download "WWDC 2012" from apple. http://events.apple.com.edgesuite.net/126pihbedvcoihbefvbhjkbvsefbg/event/index.html This script is based on previous Apple Special Event download script: https://gist.github.com/1997591
#!/usr/bin/env ruby
require 'open-uri'
size_hash = {"720p" => "6540", "720i" => "4540", "540p" => "2540", "540i" => "1840", "360p" => "1240", "360i" => "0640", "360is" => "0440", "224p" => "0240"}
if ["--help", "help", "-h"].include?(ARGV[0]) || ARGV.size < 1
puts "Usage: #{File.basename __FILE__} ( #{size_hash.keys.join(' | ')} )"
exit 0
end
@sikachu
sikachu / gist:2648731
Created May 9, 2012 20:51
Fat Radio URL
http://radio1.serverradio.net:8012/
@sikachu
sikachu / clean.rb
Created April 27, 2012 22:41
Cleanup stupid Safari bookmark duplication
require 'rubygems'
require 'nokogiri'
lines = File.open("Safari Bookmarks.html").readlines
urls = []
output = []
lines.each do |line|
if line =~ %r(<DT><A HREF="(.+)">.+</A>$)
@sikachu
sikachu / gist:1760830
Created February 7, 2012 17:25
Check validity of the gem package
tar xzfO {package}.gem metadata.gz | gzcat | grep -q '\- =' && echo "Invalid"
@sikachu
sikachu / benchmark.rb
Created December 5, 2011 01:47
Compare between not define a method in subclass and define a method in subclass and call #super
require 'benchmark'
class A0
def foo
true
end
end
(1..1000).each do |n|
eval <<-RUBY