Skip to content

Instantly share code, notes, and snippets.

View emdeeeks's full-sized avatar

Gareth Griffiths emdeeeks

View GitHub Profile
@ghn
ghn / app_form_builder.rb
Created April 28, 2023 11:18
Rails customs form Builder
# frozen_string_literal: true
# Inspiration: https://brandnewbox.com/notes/2021/03/form-builders-in-ruby/
#
# Example:
#
# f.input :field, options
#
# possible options:
# - as: symbol (:boolean, :data, :float, :radio, :select, :string, :text, :file, :time)
@amirrajan
amirrajan / main.rb
Created April 27, 2023 19:46
DragonRuby Game Toolkit - Square Fall
# game concept from: https://youtu.be/Tz-AinJGDIM
# This class encapsulates the logic of a button that pulses when clicked.
# It is used in the StartScene and GameOverScene classes.
class PulseButton
# a block is passed into the constructor and is called when the button is clicked,
# and after the pulse animation is complete
def initialize rect, text, &on_click
@rect = rect
@text = text
@hopsoft
hopsoft / 00_do_stuff_job.rb
Last active June 6, 2025 19:22
ActiveJob as Service Worker
# ActiveJob natively captures constructor arguments in an `@arguments` instance variable
# which is also exposed as an `arguments` property on each job instance.
#
# Calls to `perform_now` and `perform_later` both forward arguments to the constructor.
#
# For example, all of these invocation styles work.
#
# result = DoStuffJob.new("foobar").perform # sync
# result = DoStuffJob.new.perform("foobar") # sync
# result = DoStuffJob.perform_now("foobar") # sync
@dpaluy
dpaluy / referrable.rb
Created February 12, 2023 19:14
Referrable Rails
module Referrable
extend ActiveSupport: :Concern
included do
has_one referral, foreign_key: "referred_user_id", dependent: : destroy has_one referring_user, through: :referral alias_method :referred_by, :referring_user
has_many :referrals, foreign_key: "referring_user_id", dependent: :nullify has_many :referred_users, through: referrals
validates :referral_code, uniqueness: true, allow_nil: true
end
end
@tompng
tompng / colorize.rb
Last active August 26, 2023 08:43
福岡rubyist会議Quine
require'irb/color'
code = File.read(ARGV[0] || 'fukuokark.rb')
def hoge(s)
s.gsub("eval(s*'')","$a=s*''")
end
eval code.gsub('then{;eval', 'then{eval hoge')
w = 132
ended = false
colored = IRB::Color.colorize_code(code.lines[0].dup+'%').gsub(/\n.+/,"\n")
@Kellojo
Kellojo / WaveFunctionCollapse.cs
Created December 12, 2022 21:43
A simple wave function collapse algorithm implementation useful for procedural generation
using Kellojo.Utility;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WaveFunctionCollapse<T> where T : IWfcTile<T> {
public WfcCell<T>[,] Cells;
public int Entropy {
@ikaruga777
ikaruga777 / 0_order_state_machine.rb
Last active June 24, 2024 16:37
statesman2mermaid
class OrderStateMachine
include Statesman::Machine
state :pending, initial: true
state :checking_out
state :purchased
state :shipped
state :cancelled
state :failed
state :refunded
@amirrajan
amirrajan / main.rb
Last active February 26, 2025 21:04
DragonRuby Game Toolkit - Camera Shake (https://www.youtube.com/watch?v=-i1ILZsIqbU&ab_channel=AmirRajan)
def calc_camera
if player_charging?
if player_charge_percentage < 0.2
state.camera.trauma += 0.005
elsif player_charge_percentage < 0.5
state.camera.trauma += 0.01
elsif player_charge_percentage < 0.8
state.camera.trauma += 0.02
else
state.camera.trauma += 0.04
@matt-dray
matt-dray / perlin-dungeon-map-demo.R
Last active July 6, 2024 12:47
Quick demo of perlin noise from the {ambient} R package to generate a dungeon map
print_perlin_dungeon <- function(
m, # matrix of perlin noise via ambient::noise_perlin()
invert = FALSE # flips tile positions (use set.seed before generating noise)
) {
tile_wall = "#"
tile_floor = "."
# Standardise noise values from 0 to 1
m_bin <- round((m - min(m)) / (max(m) - min(m)))
@MiniAppleTheApple
MiniAppleTheApple / main.rb
Last active July 4, 2025 21:49
Record implementation in ruby
relative_require "record"
class Record
record :a, :b
end
a = Record.new(a: 10, b: 20)
a = a.merge(b: 10)
p a