Skip to content

Instantly share code, notes, and snippets.

View dnch's full-sized avatar

Dan Cheail dnch

  • melbourne, au
View GitHub Profile
@dnch
dnch / intro.md
Created October 11, 2010 04:42 — forked from radar/intro.md

Ruby Intro - Setlist

  • no fucking semi-colons (unless you really want them)
  • irb
  • local variable definition
  • +, *, /, - are all methods
  • puts vs print
  • multiplying strings
  • using if & when
  • defining a method
@dnch
dnch / gist:667486
Created November 8, 2010 08:46
Ruby or Perl?
Zip::ZipFile.open(f).read("data.txt").to_a.map { |x| x.chomp.split("|") }.delete_if { |x| x[2] == "KX" }.map { |a| BigDecimal(a.last) }.inject(0) { |sum, x| sum + x }
# First thing we need is the raw connection to our database. ActiveRecord makes this quite easy...
pg_connection = ModelName.connection.raw_connection
# Next, tell PG that we're going to be copying a boat load of data into model_names...
pg_connection.exec "COPY model_names (field_a, field_b, field_c) FROM STDIN"
# Process our file line-by-line
IO.foreach("path/to/file.tsv", "r") do |line|
# Convert our tab-delimited string into an array
class IncrementJumpingFormBuilder < ActionView::Helpers::FormBuilder
def fields_for_with_nested_attributes(association_name, args, block)
name = "#{object_name}[#{association_name}_attributes]"
association = args.first
if association.respond_to?(:new_record?)
association = [association] if @object.send(association_name).is_a?(Array)
elsif !association.is_a?(Array)
association = @object.send(association_name)
#!/usr/bin/env ruby
class Foo
attr_accessor :bar, :baz, :florp
def initialize(properties = {})
properties.each do |key, value|
self.send("#{key}=", value)
end
end
it "determines the controller name" do
helper.instance_eval do
stub(:params).and_return(controller: 'foos')
resource_name.should == 'foos'
namespace.should == nil
end
end
[PlanetExpress] dan: ~/Desktop [ no repo | ree-1.8.7-2010.02@test ]
> gem install rails -v=2.2.2
Successfully installed activesupport-2.2.2
Successfully installed activerecord-2.2.2
Successfully installed actionpack-2.2.2
Successfully installed actionmailer-2.2.2
Successfully installed activeresource-2.2.2
Successfully installed rails-2.2.2
6 gems installed
function mate() {
if [[ `pwd` == ~ && $1 = "." ]]; then
echo "Steadfastly refusing to open your entire home directory in TextMate."
else
/usr/local/bin/mate $1
fi
}
@dnch
dnch / gist:1069126
Created July 7, 2011 08:54
I'm losing my mind...
SELECT (field_a + field_b + field_c) AS virtual_field, *
FROM "table_name"
WHERE ("table_name".reference_id = 9999 AND (virtual_field > 0))
AND (boolean_field = 't')
ORDER BY virtual_field DESC
@dnch
dnch / gist:1084171
Created July 15, 2011 06:04
Grep Rails Routes
function grr {
if [[ -f ./Gemfile ]]; then
bundle exec rake routes | awk '{ print $1; }' | grep $1
else
rake routes | awk '{ print $1; }' | grep $1
end
}