Skip to content

Instantly share code, notes, and snippets.

@leikind
leikind / ets.ex
Created Jan 7, 2021
some ETS features
View ets.ex
test "ets match, select, and select_delete" do
ets_table_name = :bar
:ets.new(ets_table_name, [:named_table, :set, :private])
expires_at1 = ~U[2021-01-07 15:00:00.000000Z]
# ~U is just a syntax sugar for struct DateTime
IO.inspect(~U[2021-01-07 15:00:00.000000Z], structs: false)
# I would convert DateTime to unix seconds and put it as a separate field to the ets record
@leikind
leikind / render.rb
Created Apr 20, 2014
rails render and content type
View render.rb
render :inline => '<html><b>123</b></html>' #=> Content-Type: text/html; charset=utf-8
# Good
render json: {1 => 2}.to_json #=> Content-Type: application/json; charset=utf-8
# this was correct
render xml: '<xml><foo>123</foo></xml>' #=> Content-Type: application/xml; charset=utf-8
View rbenv in production
su
# Install rbenv
git clone git://github.com/sstephenson/rbenv.git /usr/local/rbenv
# Add rbenv to the path:
echo '# rbenv setup' > /etc/profile.d/rbenv.sh
echo 'export RBENV_ROOT=/usr/local/rbenv' >> /etc/profile.d/rbenv.sh
echo 'export PATH="$RBENV_ROOT/bin:$PATH"' >> /etc/profile.d/rbenv.sh
echo 'eval "$(rbenv init -)"' >> /etc/profile.d/rbenv.sh
@leikind
leikind / gist:9794053
Created Mar 26, 2014
defprotocol/defrecord example
View gist:9794053
(defprotocol Inspect (inspect [c]))
(defprotocol Maths
(plus [this another])
(multiply [this another]))
(defrecord Rational [nom den]
Inspect
(inspect [this] (str (:nom this) "/" (:den this) ))
View example.ex
case Connection.open(amqp_url) do
{:error, reason} -> # pattern matching
Logger.error(["Cannot establish connection to RabbitMQ, reason:", inspect(reason)])
{:error, reason}
{:ok, connection} -> # pattern matching
{:ok, channel} = Channel.open(connection) # pattern matching
Exchange.declare(channel, @exchange, :fanout)
View keybase.md

Keybase proof

I hereby claim:

  • I am leikind on github.
  • I am leikind (https://keybase.io/leikind) on keybase.
  • I have a public key ASDfZ5OGmfASmpWp5FyIxWP0wwPHAP3YNEp0D4Xn6SQ54go

To claim this, I am signing this object:

@leikind
leikind / upgrade.md
Last active Mar 14, 2017 — forked from chrismccord/upgrade.md
Phoenix 1.2.x to 1.3.0 Upgrade Instructions
View upgrade.md

If you want a run-down of the 1.3 changes and the design decisions behidn those changes, check out the LonestarElixir Phoenix 1.3 keynote: https://www.youtube.com/watch?v=tMO28ar0lW8

To use the new phx.new project generator, you can install the archive with the following command:

$ mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new.ez

Bump your phoenix dep

Phoenix v1.3.0 is a backwards compatible release with v1.2.x. To upgrade your existing 1.2.x project, simply bump your phoenix dependency in mix.exs:

View shapes.rb
require 'set'
module Shapes
class Coordinate < Struct.new(:x, :y)
def inspect; "#{x}:#{y}"; end
end
class Line < Struct.new(:p1, :p2)
def inspect
View auth_examples.ex
defmodule Efrontback.AuthExamples do
alias Efrontback.Device
alias Efrontback.User
alias Efrontback.Repo
use Efrontback.Web, :model
def no_schema() do
real_device_token = "abcdefg"
View ruby_poor_mans_monads.rb
# kinda library code
def pack(v)
[v]
end
class Array
def unpack
self[0]
end