Skip to content

Instantly share code, notes, and snippets.

# I just realized how easy it is to create a
# shuffled deck of cards.
# (and to create a gist - thanks github!)
def shuffled_deck
suits = %w[Hearts Clubs Diamonds Spades]
pips = %w[2 3 4 5 6 7 8 9 10
Jack Queen King Ace]
deck = suits.collect do |s|
pips.collect {|p| "#{p} of #{s}" }
=begin
My brother created a C++ app for parsing cal files (not calendars)
and outputting the final matching data as a CSV. This is my attempt
in Ruby.
Expected incoming format: some text files that match the regexp's in
the list (e.g. "value=123")
Expected output: CSV format, ordered according to the columns array.
=end
=begin
Read the db/schema.rb file and churn out a .DOT file containing
tables and linkages for *all* tables, not just the ones that
have ActiveRecord models. Guess that columns ending in _id are
links to other tables; assume all relations are one-to-many.
BACKGROUND
I'm working with a schema that is shared by a Rails app and a C++ app.
Some tables are only accessed by the C++ code and have no need to be
represented in the Rails models. A side-effect of this is that a tool
// Example markup for user edit dialog. Simple tags, with no specified class or id
// just contain an array holding their nested markup. More complex ones hold a hash
// with tag fields and a "ui" field holding their nested markup. This data would be
// recursed over to generate the expected HTML markup.
var markup = {
div: {id: 'user_dialog', ui: {
table: [
{tr: [
{td: {_class:'field_name', value:'Name'}},
{td: [
<!-- Markup generated from simple_markup.js -->
<div id='user_dialog'>
<table>
<tr>
<td class='field_name'>Name</td>
<td>
<input type='text' id='name' size=40 />
<span id='err_msg_for_name' class='err_msg' />
</td>
</tr>
var user_dialog = Dlg.run({
markup: [{
div: {id: 'user_dialog', ui: {
table: [
{tr: [
{td: {_class:'field_name', value:'Name'}},
{td: [
{input: {type:'text', id:'name', size:40,
validation: 'cannot_be_blank'}},
var user_dialog = Dialog.define({
input: {
edit: 'boolean telling us whether we are editing or adding (default)',
model: 'hash containing the current state of the user being edited',
collection: 'array of all the *other* users, for uniqueness checking',
ok_location: 'bottom (default) or top of the dialog box'
},
output: {
ok: 'boolean as to whether the user okayed their changes or not',
model: 'hash containing the updated state of the user being edited'
// Each scrap would at least define its inputs, outputs and markup.
// Additionally, it would contain any scrap-specific functionality.
// Like Rails partials, they could be chained together to build
// higher-level functionality.
var user_form = Scrap.define({
input: {
model: 'hash containing the current state of the user being edited',
collection: 'array of all the *other* users, for uniqueness checking',
},
output: {
@JohnB
JohnB / sooper_seekret_encreepshun.rb
Created March 20, 2011 16:01
Just a bit of fun...
require 'base64'
class Object
def e64 str
Base64.encode64(str).chomp
end
def d64 str
Base64.decode64 str
end
def rot13 str
rotN(13,str)
@JohnB
JohnB / Koch snowflake
Created October 27, 2011 16:36
Koch curve for Codify #codify
CODE_FOUND_AT = "https://gist.github.com/1320074        "
CREATED_BY = "      Created by john.bayor@gmail.com"
LONGLINE = 630
-- Use this function to perform your initial setup
function setup()
    print(string.format(CODE_FOUND_AT..CREATED_BY))
    iparameter("dimension",0,5)
    iparameter("sides",3,6)
    lentab = {0,0, 630, 500, 450, 450 }
    dirtab = {0,0, -120, -90, -72, -60}