Skip to content

Instantly share code, notes, and snippets.

View cpjk's full-sized avatar
🌮
🌮 🌮 🌮 🌮 🌮 🌮 🌮 🌮 🌮

Chris Kelly cpjk

🌮
🌮 🌮 🌮 🌮 🌮 🌮 🌮 🌮 🌮
View GitHub Profile
def score(dice)
score, values = 0, Hash.new(0)
dice.each {|roll_value| values[roll_value] += 1}
(2..6).each { |i| score += i*100 if values[i]>=3}
return score = score + (values[5] > 3 ? ((values[5]-3)*50) : 0) + (values[1] > 3 ? ((values[1]-3)*100) : 0) + (values[5] < 3 ? (values[5]*50) : 0) + (values[1] >= 3 ? 1000 : values[1]*100)
end
@cpjk
cpjk / slack weechat setup
Last active August 29, 2015 14:01
slack weechat setup
# Grab your credentials from https://my.slack.com/account/gateways
/server add slack yoursubdomain.irc.slack.com/6667
/set irc.server.slack.username yourusername
/set irc.server.slack.password yourircpassword
/set irc.server.slack.ssl on
alias Cooking.User
defimpl Canada.Can, for: User do
def can?(%User{ id: user_id }, action, %User{ id: user_id })
when action in [:create, :read, :show, :update, :destroy], do: true
def can?(%User{ id: user_id }, _, _), do: false
end
// controllers/home_controller.js
var homeController = {
index: function(req, res, next) {
res.render('index', { title: 'Express', fuck: 'FUCK' });
}
}
module.exports = homeController;
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
var express = require('express');
var router = express.Router();
var homeController = require('controllers/home_controller')
/* GET home page. */
router.get('/', homeController.index);
module.exports = router;
#golf
g=[];ARGF.read.chomp.split("\n")[1..-1].each_slice(2){|l,s|l=l.split.map &:to_i;s=s.split.map &:to_i;g<<{n:l[0],k:l[1],s: s}};puts g.map{|t|t[:s].inject(0){|n,q|q<=0?n+1:n}<t[:k]?"YES":"NO"}
defmodule Canary.Abilities do
defmacro can(user, actions, targets, when: condition) do # This macro throws an error when used
quote do
defimpl Canada.Can, for: unquote(user).__struct__ do
def can?(unquote(user), unquote(actions), unquote(targets)) when unquote(condition), do: true
end
end
end
defmacro can2(user, actions, targets, when: condition) do # This macro works
defimpl Canada.Can, for: User do
def can?(%User{}, :show, %Post), do: true
end
defimpl Canada.Can, for: User do # this overwrites the first implementation rather than extending it
def can?(%User{}, :delete, %Post), do: false
end
# file1.ex
defmodule A do
defmacro __using__(_env) do
quote do
Module.register_attribute __MODULE__, :rules, accumulate: true
end
end
defmacro register() do
@cpjk
cpjk / abilities.ex
Last active September 15, 2015 16:07
defmodule Abilities do
defmacro __using__(_opts) do
quote do
Module.register_attribute __MODULE__, :rules, accumulate: true
@before_compile unquote(__MODULE__)
end
end
def gather_rules(rules) do
rules