Skip to content

Instantly share code, notes, and snippets.

@yellow5
yellow5 / importer.rb
Created March 4, 2012 04:25
Ruby spec stubbing a block using mocha
class Importer
def parse_csv_file(csv_file)
File.open(csv_file) do |file|
ActiveRecordModel.transaction do
import_csv_stream(file)
end
end
end
end
@yellow5
yellow5 / foreign_key_migration.rb
Created November 10, 2011 13:31
Examples using foreigner and foreigner-matcher
class ForeignKeyMigration < ActiveRecord::Migration
def up
add_foreign_key :user_logins, :users, :dependent => :delete
end
def down
drop_foreign_key :user_logins, :users
end
end
@yellow5
yellow5 / array_to_hash.rb
Created November 1, 2011 05:16
Working example converting array of string with single divider to hash
string_array = %w( key1=val1 key2=val2 key3=val3 )
some_hash = {}
string_array.each do |array_element|
key,val = array_element.split('=')
some_hash[key.to_sym] = val
end
@yellow5
yellow5 / float19.rb
Created October 25, 2011 23:06
wip of Float#round
class Float
def round(precision=0)
if precision == 0
round_up? ? ceil : floor
else
multiple_of_ten = 10.0 ** Integer(precision).abs
if precision > 0
if finite?
number = self * multiple_of_ten
if number.infinite?
@yellow5
yellow5 / git-flow.sh
Created October 19, 2011 20:59
git flow examples
# creating a new feature
git flow feature start awesome-new-feature
# pushing to origin for other eyes
git flow feature publish awesome-new-feature
# tracking an existing remote feature branch
git flow feature track another-new-feature
# finishing a feature
@yellow5
yellow5 / debug-example1.rb
Created October 15, 2011 21:42
ruby debugger examples
class Buggy
# assuming perform_operation and special_options exist...
def buggy_method(param=nil, options={})
puts "\n\n\nDEBUG"
puts "param: #{param}"
puts "options: #{options}"
@instance_var = perform_operation(special_options(param, options))
end
end
@yellow5
yellow5 / parallel_all.rake
Created September 6, 2011 19:02
Example of extended parallel namespace within rake task
namespace :parallel do
desc "run all main specs, plugin_name specs and features in parallel with parallel:all[num_cpus]"
task :all, :count do |t, args|
Rake::Task['parallel:features'].execute(:count => args[:count])
Rake::Task['parallel:spec'].execute(:count => args[:count], :path_prefix => '../vendor/plugins/plugin_name/spec')
Rake::Task['parallel:spec'].execute(:count => args[:count])
end
end
@yellow5
yellow5 / array_total.rb
Created August 25, 2011 19:59
Error thrown via 'rake ts:conf'
class Array
def total(attr_name)
collect(&attr_name).compact.sum
end
end
@yellow5
yellow5 / dmarkow-raspell_error.sh
Created August 25, 2011 15:33
RVM + Rubinius + raspell + thinking-sphinx configuration error
cameron@cam-debian:~/projects/chc/chapp(rbx+*-)$ rake ts:conf --trace
(in /home/cameron/projects/chc/chapp)
DEPRECATION WARNING: Rake tasks in vendor/plugins/asset_packager/tasks, vendor/plugins/exception_logger/tasks, vendor/plugins/mapit/tasks, vendor/plugins/seed-fu/tasks, and vendor/plugins/state_machine/tasks are deprecated. Use lib/tasks instead. (called from __script__ at /home/cameron/.rvm/gems/rbx-master@chapp/gems/rails-2.3.14/lib/tasks/rails.rb:10)
** Invoke ts:conf (first_time)
** Invoke thinking_sphinx:configure (first_time)
** Invoke thinking_sphinx:app_env (first_time)
** Execute thinking_sphinx:app_env
** Invoke environment (first_time)
** Execute environment
rake aborted!
@yellow5
yellow5 / gem_make.out
Created August 11, 2011 20:38
rvm jruby installation error
cameron@cam-debian:~/.rvm/gems/jruby-1.6.3/gems/jruby-launcher-1.0.7-java$ cat gem_make.out
/home/cameron/.rvm/rubies/jruby-1.6.3/bin/jruby extconf.rb
make
make -f inc/Makefile-rules.mk CONF=unix SUBPROJECTS= .build-conf
make[1]: Entering directory `/home/cameron/.rvm/gems/jruby-1.6.3/gems/jruby-launcher-1.0.7-java'
make -f inc/Makefile-rules.mk jruby
make[2]: Entering directory `/home/cameron/.rvm/gems/jruby-1.6.3/gems/jruby-launcher-1.0.7-java'
mkdir -p build/unix/Linux
rm -f build/unix/Linux/argparser.o.d