Skip to content

Instantly share code, notes, and snippets.

View amitpatelx's full-sized avatar

Amit Patel amitpatelx

View GitHub Profile
@amitpatelx
amitpatelx / load.rb
Created September 8, 2023 05:29
Load any random ruby code from internet
# credit https://blog.widefix.com/fake-time-dot-now-in-production/
url = "<HTTP LINK>"
code = open(url).read
puts code
# It’s important to carefully review the code before evaluating it, to ensure it is the original,
# intended code and not something malicious.
# Only after confirming the authenticity of the code should you include it as a module:
# This can be done by raising an error if a specific line of code is not present.
@amitpatelx
amitpatelx / n1_spec.rb
Created July 4, 2023 05:13
Test N+1 in Spec
# Source: https://evilmartians.com/chronicles/squash-n-plus-one-queries-early-with-n-plus-one-control-test-matchers-for-ruby-and-rails
# Gemfile
gem 'n_plus_one_control', group: :test
# rails_helper.rb
require 'n_plus_one_control/rspec'
# spec.rb
context 'N+1', :n_plus_one do
@amitpatelx
amitpatelx / color.js
Last active September 15, 2022 12:52
// https://stackoverflow.com/questions/12043187/how-to-check-if-hex-color-is-too-black
function luma(color){
var c = color.substring(1); // strip #
var rgb = parseInt(c, 16); // convert rrggbb to decimal
var r = (rgb >> 16) & 0xff; // extract red
var g = (rgb >> 8) & 0xff; // extract green
var b = (rgb >> 0) & 0xff; // extract blue
return 0.2126 * r + 0.7152 * g + 0.0722 * b; // per ITU-R BT.709
@amitpatelx
amitpatelx / kern.log
Created December 27, 2021 14:50
Kernal Log - 20.04.3 LTS 64 bits - Auto reboot issue
This file has been truncated, but you can view the full file.
Dec 26 18:36:31 amit kernel: [ 4.629766] kvm: disabled by bios
Dec 26 18:36:31 amit kernel: [ 4.629937] EDAC amd64: F17h_M70h detected (node 0).
Dec 26 18:36:31 amit kernel: [ 4.629984] EDAC amd64: Node 0: DRAM ECC disabled.
Dec 26 18:36:31 amit kernel: [ 4.644529] [TTM] Zone kernel: Available graphics memory: 16397326 KiB
Dec 26 18:36:31 amit kernel: [ 4.644532] [TTM] Zone dma32: Available graphics memory: 2097152 KiB
Dec 26 18:36:31 amit kernel: [ 4.644539] nouveau 0000:2d:00.0: DRM: VRAM: 2048 MiB
Dec 26 18:36:31 amit kernel: [ 4.644541] nouveau 0000:2d:00.0: DRM: GART: 1048576 MiB
Dec 26 18:36:31 amit kernel: [ 4.644544] nouveau 0000:2d:00.0: DRM: TMDS table version 2.0
Dec 26 18:36:31 amit kernel: [ 4.644545] nouveau 0000:2d:00.0: DRM: DCB version 4.0
Dec 26 18:36:31 amit kernel: [ 4.644546] nouveau 0000:2d:00.0: DRM: DCB outp 00: 01000f02 00020030
@amitpatelx
amitpatelx / error.log
Created November 25, 2021 08:34
Custom Precondition for Granite
1) BA::SkillName::Update preconditions when in merge flow when merge is broken is expected not to satisfy preconditions
Failure/Error: it { is_expected.not_to satisfy_preconditions.with_message(broken_merge_msg) }
#<BA::SkillName::Update skill_name: #<ReferencesOne #<SkillName id: 1001, created_at: nil, editor_c...>, skills: #<EmbedsMany []>, remaining_skills: #<EmbedsMany [#<BA::SkillName::Update::EmbededSkill vertica...]>, skill_name_id: 1001, name: "Skill 1", skill_page_slug: "corrin-blanda-1", selected_vertical: nil, new_name: "Skill 2", after_merge_confirmation: false> does not implement: broken_merge?
@amitpatelx
amitpatelx / availability.rb
Created November 24, 2021 14:59
Working Book Example App - Rent Action with custom precondition
# frozen_string_literal: true
class Ba::Book::Availability < Granite::Action::Precondition
description 'Must not be available to rent'
def call(**)
decline_with(:unavailable) unless book_available?
end
private
@amitpatelx
amitpatelx / clone.rb
Created November 24, 2021 14:24
Error while using custom precondition
# ba/skill_name/clone.rb
...
# precondition if: -> { in_merge_flow? } do
# decline_with(precondition_error('broken_merge')) if broken_merge?
# end
precondition MergeCapabilityCheck, if: -> { in_merge_flow? }
...
...
@amitpatelx
amitpatelx / known_hosts.md
Created October 8, 2021 10:37
SSH > Search and remove host from known_hosts

ssh-keygen -H -F hostname

Or, if SSH runs on port other than 22

ssh-keygen -H -F '[hostname]:2222'

@amitpatelx
amitpatelx / memory.rb
Created October 7, 2021 05:59
Uses the ps shell command to get a rough idea of the program’s current memory size
def memstats
size = `ps -o size= #{$$}`.strip.to_i
"Size: #{size}"
end
# Source: https://www.rubytapas.com/2013/01/04/episode-042-streaming/
@amitpatelx
amitpatelx / zero.rb
Created August 26, 2021 03:46
Number of trailing zeros of N!
# https://www.codewars.com/kata/52f787eb172a8b4ae1000a34/train/ruby
# Number of trailing zeros of N!
def zeros(n)
(1..n).inject(1) {|fact, num| fact * num }.to_s.count('0')
end