Skip to content

Instantly share code, notes, and snippets.

class Foo
def bar
Bar.find(bar_id)
end
end
@jjb
jjb / gist:5043361
Last active December 14, 2015 06:29
There are a few nice guides to timezones in Rails apps (http://www.elabs.se/blog/36-working-with-time-zones-in-ruby-on-rails, http://danilenko.org/2012/7/6/rails_timezones/), but these might be overwhelming if you just want to quickly set and work with a timezone without caring about configuring it app-wide. So, here you go:
# First do this
Time.zone = 'EST'
# Now you can do this
Time.zone.now
# and this!
Time.zone.local(2013, 1, 1)
# rails/activerecord/arel work with it:
def my_method(arg1, arg2)
Foo.typecheck arg1
Bar.typecheck arg2
puts "The method finished!"
end
@jjb
jjb / 1. code.rb
Last active December 14, 2015 00:29
class Object
def self.typecheck(object)
unless self == object.class
raise ArgumentError,
"expected type: #{self}. given: #{object.class}."
end
end
end
class Foo; end
def my_method(arg1, arg2)
unless Foo == arg1.class
raise ArgumentError,
"A Foo is expected for the first argument. An #{arg1.class} was given."
end
unless Bar == arg2.class
raise ArgumentError,
"A Bar is expected for the first argument. An #{arg2.class} was given."
end
@jjb
jjb / gist:4043594
Created November 9, 2012 03:54
putting .bin in PATH only when there is a Gemfile present
function chpwd {
if [ -r $PWD/Gemfile ]; then
export PATH=./bin:${PATH//\.\/bin:}
else
regexp-replace PATH '\./bin:' ''
fi
}
@jjb
jjb / 1. without-newline.diff
Created November 4, 2012 03:51
diff dealing with absent vs. present newline when new code is appended
# diff -c 1.txt 2.txt
*** 1.txt 2012-11-03 23:24:05.000000000 -0400
--- 2.txt 2012-11-03 23:24:13.000000000 -0400
***************
*** 1,2 ****
foo
! bar
\ No newline at end of file
--- 1,3 ----
foo
@jjb
jjb / diff.diff
Created November 4, 2012 03:40
commandline tools dealing with missing trailing newline
# diff -c 1.txt 3.txt - lack of newline is announced in output
*** 1.txt 2012-11-03 23:24:05.000000000 -0400
--- 3.txt 2012-11-03 23:28:44.000000000 -0400
***************
*** 1,2 ****
! foo
bar
\ No newline at end of file
--- 1,2 ----
! food
@jjb
jjb / 1-newline.txt
Created November 4, 2012 03:31
Files for newline demo
foo
bar
2012-09-27 18:08:53.258 growlnotify[83684:707] Got disconnected: Error Domain=NSPOSIXErrorDomain Code=61 "Connection refused" UserInfo=0x7fb3e2a0f900 {NSLocalizedDescription=Connection refused, NSLocalizedFailureReason=Error in connect() function}
2012-09-27 18:08:53.260 growlnotify[83684:707] <GrowlGNTPRegistrationAttempt: 0x7fb3e2a0f770> failed because Error Domain=NSPOSIXErrorDomain Code=61 "Connection refused" UserInfo=0x7fb3e2a0f900 {NSLocalizedDescription=Connection refused, NSLocalizedFailureReason=Error in connect() function}
2012-09-27 18:08:53.260 growlnotify[83684:707] Failed to register with (null)