Skip to content

Instantly share code, notes, and snippets.

View Crisfole's full-sized avatar

Christopher Pfohl Crisfole

  • Apsis Labs
  • Beverly, MA (remote)
View GitHub Profile
@Crisfole
Crisfole / gist:1954282
Created March 2, 2012 00:32
How to create an object in a foreign assembly
using System.Reflection;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
dynamic foreignMember = CSharpCodeProvider
.CreateProvider("CSharp")
.CompileAssemblyFromSource(
new CompilerParameters
{
GenerateInMemory = true,
@Crisfole
Crisfole / gist:2763319
Created May 21, 2012 17:06
A useful drag and drop extension method
public static void SetupDragging(this ListView list)
{
bool insertAfter = false;
ListViewItem dragged = null;
ListViewItem targetItem = null;
list.DrawItem += (o, e) =>
{
e.DrawDefault = true;
if (e.Item != targetItem) return;
@Crisfole
Crisfole / Append.cs
Created July 2, 2012 20:37
Helpful C# IEnumerable extension methods
public static class AppendExtensions
{
public static IEnumerable<V> Append<V>(this V val, IEnumerable<V> enumerable)
{
yield return val;
foreach (V v in enumerable) yield return v;
}
public static IEnumerable<V> Append<V>(this IEnumerable<V> enumerable, V val)
{
@Crisfole
Crisfole / fileManipulationTemplate.linq
Created July 5, 2012 19:09
Handy linqpad template for manipulating files
void Main()
{
string fileName = ""; //Whatever the full path to the file in question is
var lines = ReadLines("");
// Often I apply some filtering/modifying as follows:
//.Skip(1) //If you need to skip some number of lines
//.Split('\t') //Or ',', or whatever, but only if you want
//.GroupBy(line => line[0]) //Again only if you want
@Crisfole
Crisfole / gist:3133791
Created July 18, 2012 02:49
Using Matrix
public static void Main()
{
Random random = new Random();
Matrix m = new Matrix(GetRandomFirstRow(random));
foreach(Note n in m.Row(0))
{
Console.Beep(n, random.Next(1, 5) * 250);
}
}
@Crisfole
Crisfole / Template.cs
Created July 18, 2012 02:56
Helpful linqpad template for file manipulation
void Main()
{
string fileName = ""; //Whatever the full path to the file in question is
var lines = ReadLines(fileName);
// Often I apply some filtering/modifying as follows:
//.Skip(1) //If you need to skip some number of lines
//.Split('\t') //Or ',', or whatever, but only if you want
//.GroupBy(line => line[0]) //Again only if you want
@Crisfole
Crisfole / jquery.disable.js
Created July 24, 2012 15:46
Add sorely lacking disable() and enable() functions to jQuery
(function ($) {
$.fn.disable = function () {
return this.each(function () {
$(this).attr("disabled", "disabled");
});
}
$.fn.enable = function () {
return this.each(function () {
$(this).removeAttr("disabled");
});
@Crisfole
Crisfole / gist:4047866
Created November 9, 2012 20:00
How to ask this question...
/*
* Input: `20 340400 1 - 20-34-04-00-00001.0-0000.00`
* Desired Output: `20 340400 1 - 2something-04different-00001.0-than-what you had`
* Repeate the above once or twice more. Preferably generalize it and only give two small examples
*/
public static string TransformData(string data) {
// ?? This is where I'm having trouble
}
module Audited
extend ActiveSupport::Concern
included do
class_attribute :options
class_attribute :operation_map
self.operation_map = {
:create => 'Created',
:update => 'Updated',
:destroy => 'Destroyed'
@Crisfole
Crisfole / base_view.js.coffee
Last active December 12, 2015 06:39
My Backbone Base View (adds a few extra handy methods to Backbone.View) Notes: 1. I always use parentheses 2. I always include `return` except: * When returning `this` at the end of the method * When the method is a single line (in which case I like to put it in-line with the method name) 3. I tend to overuse fat-arrows. I'm totally aware of thi…
class CP.Views.BaseView extends Backbone.View
# Methods for showing and hiding the view
show: () =>
@$el.show()
@
hide: () =>
@$el.hide()