Skip to content

Instantly share code, notes, and snippets.

View josevalim's full-sized avatar

José Valim josevalim

View GitHub Profile
# note that last item is User class
a = [10, [20, 30, ["helo", "world"]], [1.2, 1.3], {:foo => :bar}, User]
puts a.to_xml
<?xml version="1.0" encoding="UTF-8"?>
<objects type="array">
<object type="integer">10</object>
<object type="array">
<object type="integer">20</object>
# How to give your devise controllers all the same layout (e.g. "authentication" below)
class ApplicationController < ActionController::Base
layout :setup_layout
protected
def setup_layout
devise_controller? ? "authentication" : "application"
end
end
module UnicornPowers
def fire_super_ray_beam
self.ray_beam_power
end
end
module TomCruise
mixin UnicornPowers
def ray_beam_power
@josevalim
josevalim / index.erb
Created December 13, 2011 22:54 — forked from rafacv/index.erb
Code to get a hanging POST request on Windows using WEBRICK.
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
$(function () {
$("#myb").click(function () {
$.ajax({
url: "/showme",
type: "POST",
data: {nome: "Jose", sobrenome: "Valim"},
<% @widgets.each do |widget| %>
<p><%= widget.name %></p>
<% end %>
@josevalim
josevalim / elixir.rb
Created March 3, 2012 20:25 — forked from huffman/elixir.rb
Elixir Homebrew recipe
# Install this recipe with:
# brew install https://raw.github.com/gist/1968046/0302279edc972e9d147110bd69284d0e2becda41/elixir.rb
require 'formula'
class ErlangInstalled < Requirement
def message; <<-EOS.undent
Erlang is required to install.
You can install this with:
Expected:
[
[{"a","b"},{"c","d"}],
[{"a1","b1"},{"c1","d1"}],
[{"a2","b2"},{"c2","d2"}]
]
Actual:
def zip(a, b) do
list = to_list(a)
throw do
{ zipped, rest } = Reducers.reduce(b, { [], list }, fn
entry, { acc, [h|t] } -> { [{entry, h}|acc], t }
entry, { acc, [] } -> throw { :enum_zip, acc }
end)
:lists.foldl(fn(x, acc) ->
@josevalim
josevalim / def.exs
Last active December 20, 2015 08:19 — forked from avdi/def.exs
def next_state do
(board, x, y) ->
cell = cell_at(board, x, y)
live_count = live_neighbors(board, x, y)
next_state(cell, live_count)
(_, x, y) when (x < 0 or y < 0) ->
"."
end
def next_state do
defmodule ExCoder do
vars = [ { "&Tab;", " " },
{ "&excl;", "!" },
{ "&quot; &QUOT;", "\"" },
{ "&num;", "#" },
{ "&dollar;", "$" },
{ "&percnt;", "%" },
{ "&amp; &AMP;", "&" },
{ "&apos;", "'" },
{ "&lpar;", "(" },