Skip to content

Instantly share code, notes, and snippets.

Avatar
🙅‍♀️
permanent omnishambles

Rob Howard damncabbage

🙅‍♀️
permanent omnishambles
View GitHub Profile
View gist:817591
#ruby
[1,2,3,4].select{ |x| x.even? }
#python
[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])
#clojure
(filter #(even? % ) [1 2 3 4])
@damncabbage
damncabbage / gist:1141422
Created Aug 12, 2011
array_merge() sadness.
View gist:1141422
<?php
// 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
damncabbage / gist:1141432
Created Aug 12, 2011
$this weirdness when calling non-static function as if they were static.
View gist:1141432
<?php
//
// 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
damncabbage / gist:1148249
Created Aug 16, 2011
switch() semi-colon sadness.
View gist:1148249
<?php
$a = 'foo';
switch($a) {
case 'bar'; // <-- Yes, that's a semi-colon.
echo 'Stopped at bar.';
break;
case 'foo';
echo 'Stopped at foo.';
break;
@damncabbage
damncabbage / meta_animals.rb
Created Aug 31, 2011
Ruby Metaprogramming
View meta_animals.rb
#
# Regarding https://gist.github.com/1180947
#
require 'test/unit'
# Example classes
class Animal; end
class Dog < Animal
attr_accessor :name
@damncabbage
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
damncabbage / gist:1244530
Created Sep 27, 2011
My Authorisation Wish
View gist:1244530
# This is me
user = magical_global_get_user_method
user.id # => 123
# This is what I want to change
business.find(123)
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 = SongParser.new
begin
tree = parser.parse(input)
rescue Parslet::ParseFailed => error
puts error, parser.root.error_tree
end
@damncabbage
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
end
class Image < ActiveRecord::Base
# Has attributes: :title, :filename, :active
attr_accessible :title
@damncabbage
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| x.name }
when 'date'
proc { |x| x.date.strftime("%A, %e %B") }
end