Skip to content

Instantly share code, notes, and snippets.

View amicojeko's full-sized avatar

Stefano Guglielmetti amicojeko

View GitHub Profile
@pzac
pzac / test.rb
Created November 10, 2023 15:36
Data wrapper
class Success < Data.define(:result); end
class Failure < Data.define(:error); end
def foobar
if rand > 0.5
Success["Yes!!"]
else
Failure["Boooo"]
end
end

Introduction

Ruby on Rails is the framework of choice for web apps at Shopify. It is an opinionated stack for quick and easy development of apps that need standard persistence with relational databases, an HTTP server, and HTML views.

By design, Rails does not define conventions for structuring business logic and domain-specific code, leaving developers to define their own architecture and best practices for a sustainable codebase.

@vor0nwe
vor0nwe / clip
Last active September 13, 2023 14:59
clip: WSL bash script to access the Windows Clipboard (both read and write)
#!/bin/bash
[[ ! -t 1 ]] && powershell.exe Get-Clipboard
[[ ! -t 0 ]] && clip.exe
if [[ -t 1 && -t 0 ]]; then
echo Nothing specified to copy or paste! >&2
exit 1
fi
#!/usr/bin/env ruby
require 'etc'
require 'cgi'
require 'open-uri'
require 'nokogiri'
message = "@lleirborras is comitting "
message << Nokogiri.parse(open("http://whatthecommit.com").read).css('#content p').first.text.strip
@nicolaracco
nicolaracco / model.rb
Created March 4, 2015 10:47
has_one through setter
class User < ActiveRecord::Base
has_many :memberships, inverse_of: :user
end
# ha anche il flag owner
class Membership < ActiveRecord::Base
belongs_to :user, inverse_of: :memberships
belongs_to :group, inverse_of: :memberships
end
@marcboon
marcboon / MCP230xx.nut
Last active December 17, 2015 13:29
Encapsulates MCP23008 and MCP23017 family of I2C i/o expander for use with electric imp. Emulates the standard Pin class, allows easy moving of i/o from native pins to i/o expander pins. After creating one or more MCP23008 and/or MCP23017 class instances, user code should only access member functions of the MCP230xxPin class. See example at the …
// Base class for MCP23008 and MCP23017 family of I2C i/o expanders
class MCP230xx {
BASE_ADDR = 0x20
i2cPort = null
i2cAddr = null
regs = null
constructor(i2cPort, deviceAddr) {
this.i2cPort = i2cPort
this.i2cAddr = (BASE_ADDR + deviceAddr) << 1
@rgreenjr
rgreenjr / postgres_queries_and_commands.sql
Last active June 20, 2024 14:04
Useful PostgreSQL Queries and Commands
-- show running queries (pre 9.2)
SELECT procpid, age(clock_timestamp(), query_start), usename, current_query
FROM pg_stat_activity
WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%'
ORDER BY query_start desc;
-- show running queries (9.2)
SELECT pid, age(clock_timestamp(), query_start), usename, query
FROM pg_stat_activity
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%'
@cpjolicoeur
cpjolicoeur / gist:3590737
Created September 1, 2012 23:15
Ordering a query result set by an arbitrary list in PostgreSQL

I'm hunting for the best solution on how to handle keeping large sets of DB records "sorted" in a performant manner.

Problem Description

Most of us have work on projects at some point where we have needed to have ordered lists of objects. Whether it be a to-do list sorted by priority, or a list of documents that a user can sort in whatever order they want.

A traditional approach for this on a Rails project is to use something like the acts_as_list gem, or something similar. These systems typically add some sort of "postion" or "sort order" column to each record, which is then used when querying out the records in a traditional order by position SQL query.

This approach seems to work fine for smaller datasets, but can be hard to manage on large data sets with hundreds (or thousands) of records needing to be sorted. Changing the sort position of even a single object will require updating every single record in the database that is in the same sort group. This requires potentially thousands of wri

@marcel
marcel / gist:2100703
Created March 19, 2012 07:24
giftube – Generates an animated gif from a YouTube url.
#!/usr/bin/env ruby
# giftube – Generates an animated gif from a YouTube url.
#
# Usage:
#
# giftube [youtube url] [minute:second] [duration]
#
# ex.
#
@brentertz
brentertz / rvm2rbenv.txt
Created November 21, 2011 23:00
Switch from RVM to RBENV
## Prepare ###################################################################
# Remove RVM
rvm implode
# Ensure your homebrew is working properly and up to date
brew doctor
brew update
## Install ###################################################################