Skip to content

Instantly share code, notes, and snippets.

@vrinek
vrinek / gist:1382849
Created November 21, 2011 14:55
Convert Ruby Regexp to JavaScript RegExp
class Regexp
def to_javascript
Regexp.new(inspect.sub('\\A','^').sub('\\Z','$').sub('\\z','$').sub(/^\//,'').sub(/\/[a-z]*$/,'').gsub(/\(\?#.+\)/, '').gsub(/\(\?-\w+:/,'('), self.options).inspect
end
end
cd ~/Code/repo/ && watch -c 'ls .git/index.lock 2>/dev/null || git status --short'
@vrinek
vrinek / gist:2773707
Created May 23, 2012 07:29
Add encoding line to all spec files
enc = '# -*- encoding : utf-8 -*-'
`git ls-files -- spec | xargs head -1 | grep -v encoding`.
split("\n\n").
reject{|l| l !~ /\n/}.
map{|l| l.split("\n").first}.
map{|f| f[/^==> (.*) <==$/, 1]}.
each{|f|
system "echo '#{enc}' > tempfile && cat #{f} >> tempfile && mv tempfile #{f}"
}
# -*- encoding : utf-8 -*-
require "spec_helper"
require "set"
# Reads requests from test/fixtures/logs/requests.log and builds examples that
# test if the requests are routable.
#
# The syntax it understands is (same as `haproxy.log`):
# {www.skroutz.gr} "POST /shops/add_review/984?some=other"
#
class StaticsController < ActionController::Base
def render_404
if real_404?
send_detailed_disaster_mail(request)
end
respond_to do |format|
format.json do
render json: "", status: 404
end
if @user.try(:can_edit_post?)
# do something
end
-- Prints the variable (even a table) in detail. It is very useful for tables
-- where print() is more or less useless.
function inspect(variable, indentation)
local inspected = "" -- this will be printed at the end
local inner_indentation = indentation and indentation + 1 or 0 -- for nested tables
local tabs = "" -- also for nested tables
for i=1,inner_indentation do
tabs = tabs .. "\t"
end
@vrinek
vrinek / gist:5676786
Created May 30, 2013 09:34
Rename a method in Ruby
class Animal
def speak
puts "..."
end
end
class Cat < Animal
def speak
puts "Mew..."
end
$ git status
# On branch my-branch
# Changes not staged for commit:
# (use "git add <file>..." to update what will be committed)
# (use "git checkout -- <file>..." to discard changes in working directory)
#
# modified: db/schema.rb
#
no changes added to commit (use "git add" and/or "git commit -a")
$ git diff
@vrinek
vrinek / nested_timers.rb
Last active August 29, 2015 13:57
Dead-simple nested timers for Ruby
class MyTimerClass
attr_reader :timers
def initialize
@timers = {}
@parent_timer = nil
end
def clock!(name, &block)
@timers[name] ||= 0