Skip to content

Instantly share code, notes, and snippets.

permanent omnishambles

Rob Howard damncabbage

permanent omnishambles
View GitHub Profile
View gist:817591
[1,2,3,4].select{ |x| x.even? }
[x for x in [1,2,3,4] if not x%2]
#or, more norvingly
filter(lambda x: not x%2, [1,2,3,4])
(filter #(even? % ) [1 2 3 4])
damncabbage / gist:1141422
Created Aug 12, 2011
array_merge() sadness.
View gist:1141422
// array_merge() does terrible things to numeric keys.
// (Literal-numeric keys, that is; not necessarily everything
// that passes a is_numeric() check.)
// From the manual:
// "If the input arrays have the same string keys, then the later value
// for that key will overwrite the previous one. If, however, the arrays
// contain numeric keys, the later value will not overwrite the original
damncabbage / gist:1141432
Created Aug 12, 2011
$this weirdness when calling non-static function as if they were static.
View gist:1141432
// Call a non-static function statically from another object,
// and $this is left set as if we were still in the calling function.
class Foo {
public function notStatic() {
echo get_class($this)."\n";
damncabbage / gist:1148249
Created Aug 16, 2011
switch() semi-colon sadness.
View gist:1148249
$a = 'foo';
switch($a) {
case 'bar'; // <-- Yes, that's a semi-colon.
echo 'Stopped at bar.';
case 'foo';
echo 'Stopped at foo.';
damncabbage / meta_animals.rb
Created Aug 31, 2011
Ruby Metaprogramming
View meta_animals.rb
# Regarding
require 'test/unit'
# Example classes
class Animal; end
class Dog < Animal
attr_accessor :name
damncabbage / dinner.rb
Created Sep 15, 2011
Polite Dinner Suggestions
View dinner.rb
#!/usr/bin/env ruby
# ruby dinner.rb [number-of-dinners]
require 'rubygems';
require 'open-uri';
require 'hpricot';
recipes = Integer(ARGV[0] || 5)
damncabbage / gist:1244530
Created Sep 27, 2011
My Authorisation Wish
View gist:1244530
# This is me
user = magical_global_get_user_method # => 123
# This is what I want to change
business.owner_id # => 456, belongs to someone else
# Choose an arbitrary record, deeply tied to a Business record.
View failing_example.rb
require 'parslet'
require 'pp'
def parse(input)
parser =
tree = parser.parse(input)
rescue Parslet::ParseFailed => error
puts error, parser.root.error_tree
damncabbage / gist:1350367
Created Nov 9, 2011
Calculating Mass-Assignable ActiveRecord Attributes
View gist:1350367
class Article < ActiveRecord::Base
# Has attributes: :title, :body, :active
attr_protected :active
class Image < ActiveRecord::Base
# Has attributes: :title, :filename, :active
attr_accessible :title
damncabbage / gist:1372417
Created Nov 17, 2011
Sorting, Titles and Blocks
View gist:1372417
sort_by = 'user' # For example
column_title = case sort_by
when 'user'
proc { |x| }
when 'date'
proc { |x|"%A, %e %B") }