🙅♀️
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
# Regarding https://gist.github.com/1180947 | |
# | |
require 'test/unit' | |
# Example classes | |
class Animal; end | |
class Dog < Animal | |
attr_accessor :name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# | |
# ruby dinner.rb [number-of-dinners] | |
# | |
require 'rubygems'; | |
require 'open-uri'; | |
require 'hpricot'; | |
recipes = Integer(ARGV[0] || 5) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
OlderNewer