Skip to content

Instantly share code, notes, and snippets.

View gooh's full-sized avatar

Gordon Oheim gooh

View GitHub Profile
@gooh
gooh / responses.js
Created January 12, 2016 13:46
Swordfighting for Might Pirates; requires https://www.npmjs.com/package/hubot-response
[
{
"match": "You fight like a Dairy Farmer!",
"description": "swordfighting for might pirates",
"listener": "hear",
"response": "How appropriate! You fight like a cow!"
},
{
"match": "This is the END for you, you gutter crawling cur!",
"description": "swordfighting for might pirates",
@gooh
gooh / main.rs
Created July 27, 2015 12:37
A simple secure password generator
extern crate rand;
extern crate docopt;
extern crate rustc_serialize;
use rand::{OsRng, Rng};
use docopt::Docopt;
static USAGE: &'static str = "
Usage: rpgen [options]
rpgen --help
@gooh
gooh / gist:c8e00b99ef02a6eca602
Created June 7, 2015 10:09
Renames all files in a folder having an extension to a sequential numeric name preserving existing numerically named file names
<?php
$baseDir = $argv[1];
if (false === is_dir($argv[1])) {
die ("$baseDir is not a directory");
}
// get all files with an extension
$allFiles = array_filter(
array_map('pathinfo', glob("$baseDir/*")),
@gooh
gooh / gist:4b8f9eec685f0641834d
Created April 22, 2015 12:39
Universal Data Container
<?php
class DataCollection implements \IteratorAggregate
{
private $data;
public function __construct($data)
{
$this->data = $data;
}
### Keybase proof
I hereby claim:
* I am gooh on github.
* I am gooh (https://keybase.io/gooh) on keybase.
* I have a public key whose fingerprint is 729C 589F B492 0AE6 C22C 08F3 73BC 4368 C9EB 252E
To claim this, I am signing this object:
@gooh
gooh / randomLineGenerator
Last active December 20, 2015 08:09
Retrieve the lines in a file in random order without repeating the lines or loading the file into memory first.
function getLinesInRandomOrder($filePath)
{
$file = new SplFileObject($filePath);
$file->setFlags(SplFileObject::DROP_NEW_LINE | SplFileObject::SKIP_EMPTY);
$file->seek(PHP_INT_MAX);
$linesToTry = range(0, $file->key());
shuffle($linesToTry);
foreach ($linesToTry as $line) {
$file->seek($line);
yield $file->current();
@gooh
gooh / lint
Last active December 18, 2015 22:39
A simple linter for code snippets. Writes the snippet to a temp file and then captures and returns the output of php's commandline lint
<?php
function lint($code)
{
$code = strpos($code, '<?php') === 0 ? $code : "<?php $code";
$filename = tempnam(sys_get_temp_dir(), 'lnt');
file_put_contents($filename, $code);
exec(PHP_BINARY . " -l $filename", $errors);
unlink($filename);
<?php
for ($i = 1; $i <= 100; $i++) {
echo
$i % 3 === 0 ? 'Fizz' : '',
$i % 5 === 0 ? 'Buzz' : '',
$i % 3 && $i % 5 ? $i : '',
PHP_EOL;
};
@gooh
gooh / Attribute Reader
Created February 15, 2013 12:17
Just a proof of concept for an attribute reader trait
<?php
trait AttributeReader
{
/**
* Interceptor for non-accessible properties
*
* @param string $property
* @returns mixed
*/
public function __get($property)
@gooh
gooh / Turn all internal functions into Closures
Last active December 13, 2015 18:39
Code to turn all defined internal functions into Closures to be able to pass them around as First Class Citizens. Not that you should do that.
<?php
$functions = get_defined_functions();
foreach($functions['internal'] as $function) {
$GLOBALS[$function] = function() use ($function) {
return call_user_func_array(
$function,
func_get_args()
);
};