Simple select all.
SELECT * FROM users;User.all| # foldl() is fundamental. With a fold, you can do everything else. | |
| def foldl(list, acc, &fn) | |
| if list == [] # base case, return the accumulator | |
| acc | |
| else | |
| head, *tail = list | |
| foldl(tail, fn.call(acc, head), &fn) #recurse on the remainder | |
| end | |
| end |
| %% @doc This implements a kitchen fridge that you can put things into | |
| %% and take them out. | |
| %% | |
| %% It is stateful, through the use of recursion. | |
| %% I opted for the sets module only because I wanted to try it. | |
| %% Using the sets module has the side-effect that you can only store | |
| %% one of each food item in the fridge. | |
| -module(kitchen). | |
| -export([new/0, store/2, take/2]). |
| %% @doc Reverse Polish Notation Calculator. | |
| %% | |
| %% Parses expressions like "1 2 3 + -" = -4 | |
| %% | |
| %% This is an exercise in Learn You some Erlang for Great Good, | |
| %% however I didn't read the text and just implemented it. | |
| %% | |
| %% I guess understanding stack-based parsing helps here. | |
| -module(calc). | |
| -export([rpn/1]). |
| %% @doc A binary tree implementation in Erlang. | |
| %% A binary tree stores keys and values. | |
| -module(binary_tree). | |
| -export([init/0, init/1, insert/3, lookup/2]). | |
| -define(EMPTY_NODE, {node, 'empty'}). | |
| %% @doc Initialize an empty binary tree node. | |
| %% This is how the root of the tree should be established. | |
| %% |
| class Time | |
| class << self | |
| attr_accessor :mock_time | |
| def now_with_mock_time | |
| @mock_time || now_without_mock_time | |
| end | |
| alias_method_chain :now, :mock_time |
| class Sidekiq::Middleware::Server::GC | |
| def call(worker, msg, queue) | |
| yield | |
| ensure | |
| GC.start | |
| end | |
| end |
| class AnsiColorTest | |
| FG = 38 | |
| BG = 48 | |
| class << self | |
| def label(n, type) | |
| "\033[01;#{type};5;#{n}m %3s \033[0m" % n | |
| end | |
| def dump256 |
| /* | |
| Assuming you have an enum type like this. | |
| You want to rename 'pending' to 'lodged' | |
| */ | |
| CREATE TYPE dispute_status AS ENUM('pending', 'resolved', 'open', 'cancelled'); | |
| BEGIN; | |
| ALTER TYPE dispute_status ADD VALUE 'lodged'; | |
| UPDATE dispute SET status = 'lodged' WHERE status = 'pending'; |
| module DataMapper | |
| class Transaction | |
| def link_with_master_slave(*things) | |
| things = things.collect do |t| | |
| case t | |
| when DataMapper::Adapters::MasterSlaveAdapter | |
| t.master | |
| else | |
| t | |
| end |