Skip to content

Instantly share code, notes, and snippets.

View Yegorov's full-sized avatar
🏢
Work it harder. Make it better. Do it faster.

Artem Yegorov

🏢
Work it harder. Make it better. Do it faster.
View GitHub Profile
require 'open3'
require 'reline'
class Reline::LineEditor
private def perform_completion(list, _just_show_list)
_preposing, target, _postposing = retrieve_completion_block
namespace = IRB.CurrentContext.io.retrieve_doc_namespace(target)
namespace = namespace.first if namespace.is_a?(Array)
if namespace.nil? || !('A'..'Z').include?(namespace[0])
namespace = IRB.conf[:__MAIN__]
# In your Gemfile
gem "localhost"

# Then, depending on your desired server
gem "falcon"
gem "puma"
@julianrubisch
julianrubisch / _search.html.erb
Created June 7, 2024 10:02
Sitepress Pagefind Integration
<div data-controller="search">
<div data-search-target="button">
<div role="button" data-action="click->search#open keydown.meta+k@document->search#open keydown.ctrl+k@document->search#open" class="outline secondary search">
<%= heroicon "magnifying-glass" %>
<span>Search</span>
<kbd>Cmd/Ctrl+K</kbd>
</div>
<dialog data-search-target="dialog">
<article>
@kaspth
kaspth / concern.rb
Created June 4, 2024 20:37
Writing Concerns without the `extend`/`included`/`class_methods` boilerplate.
# Maybe there's a way to support ActiveSupport::Concern's dependencies too?
# Although this doesn't use the module hierarchy, so `prepend` can't work.
class Concern < Module
def initialize(&block) = @block = block
def included(klass) = klass.class_eval(&@block)
end
module Kernel
def Concern(...) = Concern.new(...)
end

Ruby: The future of frozen string literals

What is a literal?

In programming languages, literals are textual representations of values in the source code. This is a syntactical concept.

Some examples:

7 # integer literal
@michelson
michelson / async_method_job.rb
Created April 23, 2024 01:50
Asynchronously Concern
class AsyncMethodJob < ApplicationJob
queue_as :default
def perform(target:, method_name:, args:, queue_name: :default)
self.class.queue_as(queue_name)
# `target` could be either an instance or a class
target = target.constantize if target.is_a?(String) # Convert class name to class object if needed
target.send(method_name, *args)
end
end
@owaiswiz
owaiswiz / print_redundant_indexes.rb
Last active May 21, 2024 09:27
Print redundant indexes in a Rails app.
# Unnecessary indexes slows down writes and consumes additional storage and memory.
# Just paste this snippet in your Rails console (bundle exec rails c).
# And it will print all redundant indexes that are already covered by another index on the table:
# Table `pages`: index `site_idx` (site_id) already covered by `site_slug_idx` (site_id,slug)
# Table `optins`: index `list_idx` (list_id) already covered by `list_active_idx` (list_id,active)
ActiveRecord::Base.connection.tables.map do |table|
indexes = ActiveRecord::Base.connection.indexes(table).select(&:valid).reject(&:where)
@searls
searls / ensures_logger_broadcasts_to_stdout.rb
Last active March 4, 2024 19:18
This logger wrapper I wrote.
# Leverages the BroadcastLogger introduced in Rails 7.1 to wrap the current
# logger in a new logger that broadcasts to both the current logger and $stdout
#
# (Announcement: https://rubyonrails.org/2023/9/29/this-week-in-rails)
#
# If the current logger already broadcasts to $stdout, it will not be wrapped,
# making it safe to call this method multiple times without knowing the current
# logging "sitch".
#
# Usage probably looks something like this:
@adtac
adtac / Dockerfile
Last active May 28, 2024 01:38
#!/usr/bin/env docker run
#!/usr/bin/env -S bash -c "docker run -p 8080:8080 -it --rm \$(docker build --progress plain -f \$0 . 2>&1 | tee /dev/stderr | grep -oP 'sha256:[0-9a-f]*')"
# syntax = docker/dockerfile:1.4.0
FROM node:20
WORKDIR /root
RUN npm install sqlite3
@YOU54F
YOU54F / ruby_packagers.md
Last active December 18, 2023 20:22
State of Ruby packaging / executables - 2023

So you've got a cool app in Ruby, and you want to share it with your friends, but they don't love Ruby as much as you.

What do you do? Package it up along with the Ruby runtime and give it to them. Simple right? Maybe not so.

Thankfully smarter cookies than me, have tackled the problem, here is a map of the current state of play

  • Traveling-ruby
    • revisted for Ruby 2.6.10 -> 3.3.0-preview1 and arm64 MacOS/Windows in this fork
      • PR proposed upstream here
  • Rb2exe revisted to use the updated traveling-ruby releases in this fork