Skip to content

Instantly share code, notes, and snippets.

View mamantoha's full-sized avatar
🇺🇦
#StandWithUkraine

Anton Maminov mamantoha

🇺🇦
#StandWithUkraine
  • Ternopil, Ukraine
  • 14:04 (UTC +03:00)
View GitHub Profile
@mamantoha
mamantoha / is_port_open.cr
Created August 21, 2019 11:02
A quick way to find out if a given port is open with Crystal.
require "socket"
def is_port_open?(ip : String, port : Int32, timeout = 5)
s = TCPSocket.new(ip, port, dns_timeout: timeout, connect_timeout: timeout)
s.close
true
rescue IO::Timeout
false
end
@mamantoha
mamantoha / pry_everywhere.rb
Created November 13, 2018 07:56
Pry everywhere
# config/initializers/pry_everywhere.rb
require 'sidekiq'
# Perform Sidekiq jobs immediately in development,
# so you don't have to run a separate process.
# You'll also benefit from code reloading.
if Rails.env.development? && ENV['SIDEKIQ_INLINE'].present?
require 'sidekiq/testing'
Sidekiq::Testing.inline!
end
@mamantoha
mamantoha / polymorphic_many_to_many_in_rails.md
Created April 5, 2018 08:12
polymorphic_many_to_many_in_rails.md

Polymorphic many-to-many association in Rails

Migration

class CreateBannerCategories < ActiveRecord::Migration
  def change
    create_table :banner_categories do |t|
      t.integer :banner_id
      t.integer :category_id
@mamantoha
mamantoha / _.md
Last active November 23, 2021 13:10
Ruby goto
@mamantoha
mamantoha / uk.rb
Created April 18, 2017 18:03
Ukrainian language support for Ruby on Rails
# config/locales/defaults/uk.rb
{
uk: {
date: {
# In Ukrainian month name with day and standalone day are different
month_names: lambda do |key, options|
if options[:format] && options[:format] =~ /%-?d %B/
:'date.month_names_with_day'
else
:'date.month_names_standalone'
@mamantoha
mamantoha / query.rb
Created November 17, 2016 16:41
PostgreSQL: query to select records from last week on weekdays between 9:00 and 18:00
Model
.where(
"EXTRACT(dow FROM log_in) IN (1,2,3,4,5)"
)
.where(
"log_in::time BETWEEN '9:00' AND '18:00'"
)
.where(
"log_in BETWEEN now()::timestamp - (interval '1 week' AND now()::timestamp)"
)
@mamantoha
mamantoha / is_github_down.rb
Last active October 6, 2016 12:11
Small CLI tool to tell you if GitHub is online
require 'net/http'
def warning_message
puts <<~HEREDOC
Usage:
$ is_github_down
🦄 It's down. Go outside!
HEREDOC
@mamantoha
mamantoha / set_kde_wallpaper.rb
Last active April 16, 2018 12:26
Ruby script to change desktop wallpaper in Plasma > 5.7 from GoPro photo of the day
require 'net/http'
require 'open-uri'
require 'open3'
require 'json'
require 'optparse'
require 'tempfile'
options = {}
OptionParser.new do |opts|
opts.banner = "Usage: #{__FILE__} [options]"
@mamantoha
mamantoha / set_wallpaper
Last active February 26, 2023 21:02
Set desktop wallpaper in Plasma5 via a dbus command
qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript 'var allDesktops = desktops(); for (i = 0; i < allDesktops.length; i++) {d = allDesktops[i]; d.wallpaperPlugin = "org.kde.image";d.currentConfigGroup = Array("Wallpaper", "org.kde.image", "General"); d.writeConfig("Image", "file:///path/to/image.png")}'
fi = Float::INFINITY
p = 1
p *= 2 while 2**p < fi
a = p/2
b = p
m = a/2
p = a + m