Skip to content

Instantly share code, notes, and snippets.

View soulcutter's full-sized avatar

Bradley Schaefer soulcutter

View GitHub Profile
@soulcutter
soulcutter / each_day.rb
Last active September 4, 2015 16:32
Each day
def days(range)
Enumerator.new do |yielder|
now = range.begin
begin
yielder << now if now <= range.end
end while (now = now + 1.day) <= range.end
end
end
days((Time.now - 3.days)..(Time.now)).each { |day| puts day }
@soulcutter
soulcutter / gist:2314814
Created April 5, 2012 22:52
Overriding instance methods
module WizardSkills
module InstanceMethods
def trick
super
puts "The #{self.class} turned a man into a rabbit"
end
end
end
module MagicianSkills
@soulcutter
soulcutter / gist:2458278
Created April 22, 2012 05:44
irbrc helper to print documentation
class Object
# list methods which aren't in superclass
def local_methods(obj = self)
(obj.methods - obj.class.superclass.instance_methods).sort
end
# print documentation
#
# ri 'Array#pop'
# Array.ri
@soulcutter
soulcutter / gist:2477448
Created April 24, 2012 07:26
Saxerator example
require 'saxerator'
# The parser takes any IO object (or String) - a File is a common choice
parser = Saxerator.parser(bookshelf_xml)
# Simplest usage, parses every <book></book> in the document
parser.for_tag(:book).each do |book|
# Your book hash might look something like
{ 'title' => 'Rails 3 In Action', 'author' => ['Ryan Bigg', 'Yehuda Katz'] }
end
describe Article do
subject(:article) { Article.new }
it "validates presence of :title" do
article.should validate_presence_of(:title)
end
end
@soulcutter
soulcutter / gist:3671893
Created September 8, 2012 04:48
JRuby NPE using Symbol#to_proc with --1.9
➜ saxerator git:(master) ✗ ruby -v
jruby 1.7.0.preview2 (1.9.3p203) 2012-09-07 f4306fa on Java HotSpot(TM) 64-Bit Server VM 1.6.0_33-b03-424-11M3720 [darwin-x86_64]
➜ saxerator git:(master) rspec spec/lib/dsl/for_tag_spec.rb
/Users/soulcutter/.rvm/gems/jruby-head@saxerator/gems/rspec-core-2.11.1/lib/rspec/core/example.rb:274 warning: singleton on non-persistent Java type Java::JavaLang::NullPointerException (http://wiki.jruby.org/Persistence)
F
Failures:
1) Saxerator::DSL#for_tag should only select the specified tag
Failure/Error: Unable to find matching line from backtrace
@soulcutter
soulcutter / gist:3718477
Created September 13, 2012 23:11
ssh-agent launcher
SSH_ENV="$HOME/.ssh/environment"
function start_agent {
echo "Initializing new SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
echo succeeded
chmod 600 "${SSH_ENV}"
ssh_environment_init;
/usr/bin/ssh-add;
}
@soulcutter
soulcutter / gist:3738320
Created September 17, 2012 16:27
Adding magic encoding comments to ruby files
#!/bin/zsh
for file in $(awk 'FNR == 1 && !/^#\!/ { print FILENAME; nextfile }' **/*.rb); do
echo $file
sed -i 1i'# encoding: utf-8' $file
done
@soulcutter
soulcutter / gist:3873662
Created October 11, 2012 16:33
Spring ActiveProfiles injection question
// Question: Can you do something like this without using interface-based injection?
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("test")
public class FakeDownloadUtilTest extends BaseTest {
@Autowired
DownloadUtil downloadUtil;
@Test
public void testFaked() {
@soulcutter
soulcutter / gist:4003852
Created November 2, 2012 19:37
Installing savon under jruby 1.6.3
➜ ~ rvm install jruby-1.6.3
jruby-1.6.3 - #downloading jruby-bin-1.6.3, this may take a while depending on your connection...
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 14.7M 100 14.7M 0 0 459k 0 0:00:32 0:00:32 --:--:-- 410k
jruby-1.6.3 - #extracting jruby-bin-1.6.3 to /Users/brschaefer/.rvm/src/jruby-1.6.3
jruby-1.6.3 - #extracted to /Users/brschaefer/.rvm/src/jruby-1.6.3
Building Nailgun
jruby-1.6.3 - #installing to /Users/brschaefer/.rvm/rubies/jruby-1.6.3
jruby-1.6.3 - adjusting #shebangs for (jrubyc jirb_swing jirb jgem rdoc ri spec autospec testrb ast generate_yaml_index.rb).