Skip to content

Instantly share code, notes, and snippets.

View paulsonkoly's full-sized avatar

Paul Sonkoly paulsonkoly

View GitHub Profile
@paulsonkoly
paulsonkoly / x.rb
Created February 8, 2021 12:28
singleton
class Foo
def initialize
singleton_class.class_eval { def foo; :bar; end }
singleton_class.class_eval do
alias_method :f_o_o, :foo
end if respond_to? :foo
end
end
@paulsonkoly
paulsonkoly / x.rb
Last active February 6, 2021 19:18
pokemon
describe '#reload' do
# Attributes
let(:id) { 25 }
let(:name) { 'Pikachu' }
let(:new_name) { 'Pika' }
let(:pokemon) { Pokemon.find(id) }
it 'returns an object with the original attributes' do
pokemon.name = new_name
@paulsonkoly
paulsonkoly / x.rb
Created July 11, 2020 16:33
local var assignment
def foo
13
end
2.times do
p foo
foo = 'something else'
p foo
end
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'gosu'
def linear_map(value, mina, maxa, minb, maxb)
(maxb - minb) * (value - mina) / (maxa - mina) + minb
end
@paulsonkoly
paulsonkoly / x.rb
Last active June 16, 2020 15:41
sandstorm
require 'tempfile'
module Music
refine Float do
def hz
self
end
def khz
self * 1000
@paulsonkoly
paulsonkoly / y.rb
Created March 16, 2020 20:56
rspec
require 'rspec/autorun'
RSpec::Matchers.define :be_one_of do |expected|
match do |actual|
expected.include? actual
end
end
RSpec.describe 13 do
it 'is in the list of allowed values' do
@paulsonkoly
paulsonkoly / x.rb
Created January 27, 2020 17:42
private stub
require 'rspec/autorun'
class X
def call
fund_query
end
private
def fund_qeuery
@paulsonkoly
paulsonkoly / x.rb
Created January 22, 2020 16:59
validators
require 'active_support'
require 'active_model'
class X
include ActiveModel::Validations
include ActiveModel::Attributes
attribute :blah
validates :blah, numericality: { greater_than_or_equal_to: 0 }
@paulsonkoly
paulsonkoly / y.rb
Last active December 11, 2019 10:47
validator
require 'active_support'
require 'active_model'
class Form
include ActiveModel::Validations
include ActiveModel::Attributes
attribute :blah
validates :blah, numericality: true
end
@paulsonkoly
paulsonkoly / sudoku.rb
Last active April 13, 2020 07:25
optimized
require 'forwardable'
# Board is a 9x9 standard sudoku board. This class does not care about what is
# stored in each cell of the board, it merely provides indexing and enumeration
# methods including accessing rows, columns, or 3x3 sudoku boxes.
class Board
extend Forwardable
# @param data [Array] flat array of 81 initial cell values
def initialize(data)