Skip to content

Instantly share code, notes, and snippets.

@budu
budu / llm.rb
Created February 18, 2025 20:58
if Rails.env.development?
require 'langchain'
LLM_CLIENT ||= Langchain::LLM::OpenAI.new(
api_key: ENV.fetch('CHATGPT_API_KEY'),
default_options: {
temperature: 0.2, # Lower temperature for deterministic output
chat_model: 'gpt-4o'
}
)
Langchain.logger.level = Logger::DEBUG
@budu
budu / wls.rb
Created January 23, 2025 02:46
Implementation of statistical regression analysis (OLS and WLS)
#!/usr/bin/env ruby
# rubocop:disable all
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'numo-linalg'
gem 'rover-df'
end
@budu
budu / clock.rb
Created March 15, 2024 03:59
Quick Ruby implementation of Exercism Clock exercise
Clock = Data.define :hours, :minutes do
def initialize(hours:, minutes:)
super hours: (hours + minutes / 60) % 24, minutes: minutes % 60
end
def add_minutes(minutes) = self.class.new(hours, self.minutes + minutes)
def to_s = '%02d:%02d' % [hours, minutes]
end
@budu
budu / example.json
Created April 21, 2023 16:27
Stay example
{
"stay": {
"check_in": "2014-07-08",
"check_out": "2014-07-09",
"participants": {
"0": {
"variant_id": "2",
"count": "2"
},
"1": {
@budu
budu / console.rb
Created November 3, 2022 15:14
DST fun!
> Time.new(2022,11,6) + 3.hours
=> 2022-11-06 02:00:00 -0500
> Time.new(2022,11,6) + 3.hours.in_days.days
=> 2022-11-06 02:00:00 -0500
> Time.new(2022,11,6) + 24.hours.in_days.days
=> 2022-11-07 00:00:00 -0500
> Time.zone.local(2022,11,6) + 3.hours
=> Sun, 06 Nov 2022 02:00:00 EST -05:00
> Time.zone.local(2022,11,6) + 3.hours.in_days.days
=> Sun, 06 Nov 2022 03:00:00 EST -05:00
@budu
budu / fb.css
Created October 8, 2020 15:47
Quick fix for FB desktop layout
/* move middle part of top nav on the side */
div[role="banner"] > div:nth-child(3) {
right: auto;
top: 70px;
}
/* remove extra padding from main nav */
div[role="banner"] > div:nth-child(3) ul {
padding: 0;
}
@budu
budu / descendants_tracker.rb
Created January 6, 2019 23:21
Modified DescendantsTracker that doesn't leak memory (but might cause other problems)
# see https://github.com/rails/rails/pull/31442
require 'weakref'
module ActiveSupport
# This module provides an internal implementation to track descendants
# which is faster than iterating through ObjectSpace.
module DescendantsTracker
@@direct_descendants = {}
@budu
budu / find_needles_in_haystack.rb
Created January 6, 2019 23:17
Useful script to help debugging memory leaks
# This script is taken from https://github.com/rails/rails/issues/32892#issuecomment-423376552
require 'ap'
require 'json'
require 'set'
require 'cgi'
require 'byebug'
class Slot
attr_reader :info, :address_to_slot
@budu
budu / enumerable.rb
Created April 25, 2018 22:36
Cute way to get frequency of items in an enumerable with some bonus methods on the resulting hash.
Enumerable.module_eval do
def frequency
Hash.new(0).tap do |h|
each { |v| h[v] += 1 }
h.define_singleton_method(:highest) { sort_by(&:last).last.first }
h.define_singleton_method(:lowest) { sort_by(&:last).first.first }
end
end
end
@budu
budu / object.rb
Created April 25, 2018 22:34
When you need to figure out what methods are defined at which point in the class hierarchy.
Object.class_eval do
def hierarch(n = 1)
klass_and_ancestry = self.class.ancestors[(n - 1)..-1]
klass, *ancestry = klass_and_ancestry
to_klass_methods = methods - ancestry.flat_map(&:instance_methods)
"#{klass}: #{to_klass_methods}"
end
end