Skip to content

Instantly share code, notes, and snippets.

View timuruski's full-sized avatar
🥊
Hit. Don't get hit.

Tim Uruski timuruski

🥊
Hit. Don't get hit.
View GitHub Profile
@timuruski
timuruski / http_request_spec.rb
Created January 13, 2021 22:41
Example of a flexible pattern for making HTTP requests with Ruby Net:HTTP
require "json"
require "net/http"
require "uri"
# The standard library Net::HTTP module is a bit convoluted, but here is an example of a flexible
# system for making different kinds of HTTP requests. To see some convenience methods you can
# consult the docs: https://ruby-doc.org/stdlib-2.5.5/libdoc/net/http/rdoc/Net/HTTP.html
class HttpConnection
def get(url, params = nil)
@timuruski
timuruski / block_binding.rb
Last active September 8, 2023 19:46
Demonstration of block binding differences in Ruby
class Expectation
def to(matcher = nil, &block)
if block_given?
puts "block passed to `to` method"
yield
end
end
end
def expect
@timuruski
timuruski / pre-commit.rubocop
Last active February 1, 2019 13:10
Pre-Commit Rubocop check
#!/bin/sh
# Simplified from this script, (less robust and more to the point):
# http://gmodarelli.com/2015/02/code-reviews-rubocop-git-pre-commit/
# Installation: copy this code into <REPO>.git/hooks/pre-commit.sh
# Select only staged Ruby files
FILES="$(git diff --cached --name-only --diff-filter=AMC | grep "\.rb$" | tr '\n' ' ')"
@ohanhi
ohanhi / frp.md
Last active May 6, 2024 05:17
Learning FP the hard way: Experiences on the Elm language

Learning FP the hard way: Experiences on the Elm language

by Ossi Hanhinen, @ohanhi

with the support of Futurice 💚.

Licensed under CC BY 4.0.

Editorial note

@carols10cents
carols10cents / ruby-to-rust-cheat-sheet.md
Last active November 24, 2020 23:12
Ruby to Rust Cheat Sheet

Ruby to Rust Cheat Sheet

The goal of this is to have an easily-scannable reference for the most common syntax idioms in Ruby and Rust so that programmers most comfortable with Ruby can quickly get through the syntax differences and feel like they could read and write basic Rust programs.

What do you think? Does this meet its goal? If not, why not?

Variables

Ruby:

@timuruski
timuruski / compress.rb
Last active December 14, 2015 18:27
A dumb algorithm for compressing blocks of sorted integers, like row IDs.
# This algorithm takes a *sorted* list of integers, probably record
# IDs and compresses them into a series of ranges. It also includes a
# verification check that no values were lost.
def compress(ary)
range_start = ary.first
compressed = []
ary.each_with_index do |value, index|
# TODO: This could skip indices that have already been considered.

First, you install ruby-build and chruby. ruby-build is a program that knows how to download and build different ruby versions. chruby manages $PATH to control which ruby gets invoked in your shell. They work completely independently.

sudo su
cd /usr/src

git clone https://github.com/sstephenson/ruby-build.git
cd ruby-build
./install.sh
cd -
@timuruski
timuruski / nested_exception.rb
Last active January 1, 2016 18:59
Nested exception mixin for Ruby
# Adapted from from: http://nestegg.rubyforge.org/
module NestedException
# Public: Initializes a new nested error, the original error defaults
# to the global error variable so that it doesn't need to be explicity
# provided when re-raising an exception.
def initialize(message = nil, cause = $!)
@cause = cause
super(message || cause && cause.message)
end
<?php
class Curl {
const HEAD = 'POST';
const GET = 'GET';
const POST = 'POST';
const PUT = 'PUT';
const DELETE = 'DELETE';
const LOCK = 'LOCK';
const UNLOCK = 'UNLOCK';

Braces

Don't use newline braces.

<?php

// Good:

if ( $foo ) {