Skip to content

Instantly share code, notes, and snippets.

View amolbrid's full-sized avatar

Amol Brid amolbrid

View GitHub Profile
@amolbrid
amolbrid / database.yml
Created November 22, 2010 02:09
YAML database configuration file
host: localhost
adapter: postgresql
database: learn_db
username: learn
password: learn
pool: 5
timeout: 5000
@amolbrid
amolbrid / gist:709442
Created November 22, 2010 02:36
Using ActiveRecord without Rails
require 'rubygems'
require 'active_record'
dbconfig = YAML::load(File.open('database.yml'))
ActiveRecord::Base.establish_connection(dbconfig)
ActiveRecord::Base.logger = Logger.new(File.open('database.log', 'a'))
class Employee < ActiveRecord::Base
end
# ----------- Set prompt to show working git branch ------------------
# inspired by fowlduck's http://gist.github.com/4477
# unfortuneately if I echo ansi codes it messes with line wrapping on the
# command line,
# guessing because unlike ansi codes in a VAR it doesn't know the real size of
# the string when echo'd :(
# doesn't matter if the codes are set using tput or straight up raw ansi
function git_prompt {
BRANCH=`git branch 2> /dev/null | sed -e '/^[^*]/d' -e "s/* \(.*\)/\1/"`
if [ -n "${BRANCH}" ]; then
@amolbrid
amolbrid / freeze_string.rb
Created November 26, 2010 01:29
ruby freeze string
str = "Can you modify me?"
str.freeze
str << " I'm trying." #this will raise TypeError
@amolbrid
amolbrid / frozen_test.rb
Created November 26, 2010 01:36
frozen? method example
str = "Can you modify me?"
str.freeze
if str.frozen?
p "cannot modify string"
else
str = "string modified"
p str
end
@amolbrid
amolbrid / freeze_var.rb
Created November 26, 2010 02:17
freeze instance, not variable pointing to instance
class FreezeTest
def initialize(str)
@text = str
end
end
obj1 = FreezeTest.new("obj1")
obj2 = FreezeTest.new("obj2")
obj3 = obj1
@amolbrid
amolbrid / dup_clone_diff.rb
Created November 28, 2010 02:34
dup and clone difference
str = "Hello"
str.freeze
str_clone = str.clone
puts "str_clone is still frozen" if str_clone.frozen?
str_dup = str.dup
puts "str_dup is not frozen" unless str_dup.frozen?
# output:
@amolbrid
amolbrid / RequestProcessor.java
Created December 7, 2010 15:17
request processor class for Mockito inOrder invocation
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.Socket;
public class RequestProcessor {
private Socket socket;
public RequestProcessor(Socket socket) {
this.socket = socket;
@amolbrid
amolbrid / RequestProcessorTest.java
Created December 7, 2010 15:24
RequestProcessorTest class for Mockito invocation order blog post
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.*;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.Socket;
import junit.framework.Assert;
@amolbrid
amolbrid / EstTimezone.java
Created December 7, 2010 21:30
snippet to convert date/time in any timezone to EST
public static Calendar convertTimeToEST(long timeInMillSec) {
Calendar cal = Calendar.getInstance();
TimeZone tz = cal.getTimeZone();
cal.setTimeInMillis(timeInMillSec);
TimeZone estTz = TimeZone.getTimeZone("America/New_York");
Calendar estCal = Calendar.getInstance(estTz);
estCal.setTimeInMillis(timeInMillSec);
int tzOffset = tz.getOffset(cal.getTime().getTime());
int estTzOffset = estTz.getOffset(estCal.getTime().getTime());