Skip to content

Instantly share code, notes, and snippets.

View esquinas's full-sized avatar

Enrique Esquinas esquinas

  • Asturias, Spain
View GitHub Profile
@esquinas
esquinas / factorial.rb
Created May 12, 2016 19:08
Simplest factorial function in Ruby
# Add factorial method to Math standard module in Ruby
module Factorial
def factorial(n)
Integer Math.gamma(n.to_i + 1)
end
end
Math.extend Factorial
@esquinas
esquinas / fix_active_support_pluralize.rb
Created June 6, 2016 17:41
ActiveSupport cannot pluralize 'human' properly
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.plural('human', 'humans')
inflect.plural('cayman', 'caymans')
end
@esquinas
esquinas / my_pluralize.rb
Created June 6, 2016 17:44
Quick and dirty pluralization in Ruby
# CAUTION, it opens String class
String.class_eval do
# HACK: quick and dirty english pluralization.
def to_plural(*arg)
plural = lambda do |str|
if str =~ /\s\z|men\z|people\z|[^aious]s\z/i # is a plural already
str
elsif str =~ /\Aman\z|woman\z/i # man & woman, except caiman, human...
str.sub(/an\z/i, 'en')
elsif str =~ /[^aeiou][aeiou]?(f|fe)\z/ # knife, half...
@esquinas
esquinas / formatISOAPA.js
Last active October 3, 2016 11:37
ISO (SI) & APA style number formatting are used here to make a fail-safe, unambiguous way to render numbers for international and multicultural contexts.
// Number.formatISOAPA returns a correctly SI+APA formatted number.
// (SI, System Internationale) ISO 31-0 & APA style EXPLANATION:
// <https://en.wikipedia.org/wiki/ISO_31-0#Numbers>
// ISO 31-0: The decimal sign is either the comma on the line
// or the point on the line. Both "1,33" & "1.33" are valid.
// ISO 31-0: Do not use "." nor "," for thousand separators.
// Both "1,000.00" "1.000,00" are INVALID.
// ISO 31-0: For numbers whose magnitude is less than 1, the decimal
// sign should be preceded by a zero. "0.5" instead of ".5"
// APA style: Psychologically, there is no need to group numbers with
@esquinas
esquinas / countdown_class.rb
Created September 4, 2016 13:22
Countdown ruby class is for making countdown ranges without #reverse
# Countdown class is for making countdown ranges without reversing.
# a = Countdown.new 5
# b = Countdown.new 1
# print (a..b).map(&:value).join(', ') # =>
# 5, 4, 3, 2, 1 => nil
class Countdown
VERSION = 0
attr_accessor :value
def initialize(value)
@esquinas
esquinas / caesar_cipher.rb
Created October 3, 2016 11:30
My Ruby implementation of a simple Caesar cipher
# Caesar cipher module
#
# USAGE:
# =====
# ```ruby
# my_var = Plaintext.new 'TOO MANY SECRETS'
# my_var.rotn # => "GBB ZNAL FRPERGF"
# my_var # => "TOO MANY SECRETS"
# ```
# There is a bang method to make changes permanent:
@esquinas
esquinas / bill_gospers_troublesome12.js
Created January 18, 2018 17:25
P5js implementation of the "Twubblesome Twelve Circle Packing Puzzle Problem" designed by Bill Gosper
// Can be run online at https://p5js.org/ or alpha.editor.p5js.org/full/SykVmQWVG
const SCREEN = {
width: 800,
height: 600 }
const T12 = {
radii: [5.943, 7.290, 8.365, 11.315, 12.576, 14.497, 13.067, 12.151, 8.788, 7.941, 6.383, 5.495],
cavity: 38.149 }
const FACTOR = 4
var myCircles
@esquinas
esquinas / google-sheet-visibility.gs
Last active January 3, 2023 18:54
Check visibility of hidden rows and columns in Google Spreadsheet App Script
/*
USAGE EXAMPLES:
Is the tenth row hidden?
> isRowHidden(sheet.getRange('B10'))
Is column B hidden?
> isColumnHidden(sheet.getRange('B10'))
Is cell B10 visible? (not in a hidden row and/or column)
@esquinas
esquinas / install-red.sh
Created March 23, 2018 18:19
C9.io Installation of the Red Language. +Info on "red-lang.org"
#!/usr/bin/env bash
# C9.io Installation of the Red Language. +Info on "red-lang.org".
## USAGE: `chmod +x ./install-red.sh && sudo ./install-red.sh`
REDLANG_PATH="/usr/local"
# For Linux 64-bit distros, you need to install 32-bit supporting libraries.
# So, for Debian-based distros, install them using:

Exercises

  • What order (Invoice) was the most expensive?
      SELECT Invoice.InvoiceId, MAX(Invoice.Total)
      FROM Invoice;
    Total: 25.86,
    InvoiceId: 404,
    CustomerId: "6",