Skip to content

Instantly share code, notes, and snippets.

View cpjk's full-sized avatar
๐ŸŒฎ
๐ŸŒฎ ๐ŸŒฎ ๐ŸŒฎ ๐ŸŒฎ ๐ŸŒฎ ๐ŸŒฎ ๐ŸŒฎ ๐ŸŒฎ ๐ŸŒฎ

Chris Kelly cpjk

๐ŸŒฎ
๐ŸŒฎ ๐ŸŒฎ ๐ŸŒฎ ๐ŸŒฎ ๐ŸŒฎ ๐ŸŒฎ ๐ŸŒฎ ๐ŸŒฎ ๐ŸŒฎ
View GitHub Profile
@cpjk
cpjk / np.py
Last active February 12, 2016 01:21
class Solution:
def connect(self, root):
self.do_connect(root, None)
def do_connect(self, node, right_sib):
if not node:
return
node.next = right_sib if right_sib else None
if node.left:
self.do_connect(node.left, right_sib=node.right)
class Node
attr_accessor :next, :left_child, :right_child
end
def add_next_right_ptrs(node, right_sib)
node.next = right_sib || nil
if node.left_child
add_next_right_ptrs(node.left_child, right_sib=node.right_child)
end
if node.right_child
def palindrome?(s)
s = s.downcase.split(/[^a-zA-Z0-9]+/).join.split ""
s.zip(s.reverse).each do |letters|
return false if letters[0] != letters[1]
end
return true
end
require 'net/http'
require 'json'
HOST = 'shopicruit.myshopify.com'
PRODUCT_PATH = '/products.json'
def get_computer_and_keyboard_cost()
json_str = Net::HTTP.get HOST, PRODUCT_PATH
json_dict = JSON.parse json_str
defmodule Abilities1 do
defimpl Canada.Can, for: User do
def can?(%User{id: user_id}, action, %Testimonial{id: testimonial_id})
when action in [:create], do: true
def can?(%User{id: user_id}, action, %Testimonial{id: testimonial_id})
when action in [:edit], do: true
end
end
defmodule BetterAbilities do
@cpjk
cpjk / abilities.ex
Last active November 25, 2015 04:34
defmodule Blog.Abilities do
defimpl Canada.Can, for: Blog.User do
def can?(%Blog.User{}, :index, Blog.User), do: true
def can?(%Blog.User{}, _, _), do: true
def can?(nil, _, _), do: false
@cpjk
cpjk / s.ex
Created September 15, 2015 16:07
require Abilities
defmodule User do
defstruct id: 1, dude_id: 1
end
defmodule M do
use Abilities
@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
# file1.ex
defmodule A do
defmacro __using__(_env) do
quote do
Module.register_attribute __MODULE__, :rules, accumulate: true
end
end
defmacro register() do
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