Skip to content

Instantly share code, notes, and snippets.

View damncabbage's full-sized avatar
🙅‍♀️
permanent omnishambles

Robin Howard damncabbage

🙅‍♀️
permanent omnishambles
View GitHub Profile
#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 August 12, 2011 03:59
array_merge() sadness.
<?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 August 12, 2011 04:06
$this weirdness when calling non-static function as if they were static.
<?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 August 16, 2011 01:14
switch() semi-colon sadness.
<?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 August 31, 2011 11:23
Ruby Metaprogramming
#
# 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 September 15, 2011 03:02
Polite Dinner Suggestions
#!/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 September 27, 2011 07:25
My Authorisation Wish
# 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.
@damncabbage
damncabbage / failing_example.rb
Created October 11, 2011 12:59
Parslet Woes
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 November 9, 2011 04:21
Calculating Mass-Assignable ActiveRecord Attributes
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 November 17, 2011 05:13
Sorting, Titles and Blocks
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