Skip to content

Instantly share code, notes, and snippets.

@Bodacious
Bodacious / Benchmark semver ordering in Postgresql.rb
Last active March 16, 2024 23:01
Benchmarking different SEMVER ordering strategies in PostgreSQL
require 'bundler/inline'
##
# Get dependencies
gemfile do
source 'https://rubygems.org'
gem 'pg'
gem 'activerecord', require: 'active_record'
gem 'benchmark-ips'
gem 'pry'
@Bodacious
Bodacious / Gemfile
Created January 10, 2023 21:10
Demonstrating the anonymous modules when including blocks in scopes
# frozen_string_literal: true
source "https://rubygems.org"
gem "activerecord", github: "bodacious/rails"
gem "sqlite3"
@Bodacious
Bodacious / main.rb
Created June 28, 2022 10:38
Jbuilder named attributes vs `extract!`
require "bundler/inline"
gemfile do
gem "benchmark-ips"
gem "jbuilder"
gem "activesupport", require: ["active_support"]
end
class User
attr_accessor :id, :name, :email, :password, :age, :username
@Bodacious
Bodacious / README.md
Created January 24, 2012 20:29
New York Address generator

NY Address Generator

60% of the time it works EVERY time!

Installing

  gem 'ny_address', git: 'git://gist.github.com/1672379.git'
@Bodacious
Bodacious / pesapal.rb
Created May 22, 2013 09:52
PesaPal Implementation in Ruby (test)
module PesaPal
require 'active_support'
include ActiveSupport::Configurable
self.configure do |config|
config.test_mode = true
config.consumer_key = "<your key>"
config.consumer_secret = "<your secret>"
end
@Bodacious
Bodacious / string.rb
Created September 11, 2020 15:55
Ruby Palindrome String
class String
def palindrome?
clean_string = self.gsub(/[^\w]/, '').downcase
clean_string == clean_string.reverse
end
end
puts "madam".palindrome? # => true
puts "racecar".palindrome? # => true
puts "madam, . ".palindrome? # => true
puts "02/02/2020".palindrome? # => true
@Bodacious
Bodacious / datetime.rb
Created March 2, 2012 10:50
Custom date/time formats in Ruby on Rails
[Time, Date].map do |klass|
klass::DATE_FORMATS[:variable] = lambda do |t|
date =
case
# display today's date as "today"
when t >= Date.today then "today"
# display yesterday's date as 'Yesterday'
when (t >= Date.yesterday and t <= Date.today) then "yesterday"
# otherwise, display date: eg. Mon 4th June
else
@Bodacious
Bodacious / Katana Code's Git Strategy.md
Created April 14, 2013 18:38
Here's KatanaCode's Git Strategy which each of us use across all of the projects we're involved with

Katana Code's Git Strategy

Introduction

The purpose of this document is to define the strategy which all Katana Code employees and subcontractors must adhere to when working on Katana Code projects.

The aim is to develop a uniform, coherent and effective solution. This document is not written in stone and can be updated as and when better approaches are discovered.

Topic Branches

@Bodacious
Bodacious / monty_hall.rb
Created May 31, 2018 17:00
Monty Hall Problem demonstrated in Ruby
class Game
attr_accessor :doors
attr_reader :correct_door
def initialize(door_count: )
chosen_door = (1..door_count).to_a.sample
@doors = door_count.times.map.with_index do |d, i|
Door.new(i+1 == chosen_door)
end
@correct_door = @doors.detect(&:correct?)
end
@Bodacious
Bodacious / base.rb
Created August 20, 2012 17:56
An "ActiveRecord" library for RubyMotion
module ActiveRecord
class Base
COLUMN_TYPES_MAPPING = {
:int => 'INTEGER',
:long => 'INTEGER',
:longLongInt => 'BIGINT',
:bool => 'BOOLEAN',
:double => 'DECIMAL',