Skip to content

Instantly share code, notes, and snippets.

View toy's full-sized avatar
🇺🇦

Ivan Kuchin toy

🇺🇦
  • OpenProject (@opf)
  • Hamburg, Deutschland
View GitHub Profile
@toy
toy / README.md
Last active April 12, 2025 14:58
Self contained rake application, no Rakefile needed
./binary_name # run default task
./binary_name greeting # run greeting task
./binary_name -T # list tasks
./binary_name -h # help

Or put on PATH and use binary_name …

@toy
toy / _arc.rb
Created April 8, 2023 21:40
Archive exercism.io solutions
#!/usr/bin/env ruby
require 'time'
require 'bundler/inline'
gemfile do
source 'https://rubygems.org'
gem 'faraday'
@toy
toy / .gitignore
Last active September 16, 2022 23:25
Comparison of image_size with fastimage for remote image
/random.*
/*.gem
/Gemfile.lock
/data/
@toy
toy / co2monitor
Last active November 17, 2020 20:17
Getting data from CO2 monitor translated to ruby and posting to graphite
#!/usr/bin/env ruby
require 'logger'
require_relative 'graphite'
# https://hackaday.io/project/5301-reverse-engineering-a-low-cost-usb-co-monitor
class CO2Reader
MAGIC_BYTES = 'Htemp99e'.bytes.map{ |b| ((b >> 4) | (b << 4)) & 0xff }
MAGIC_ORDER = [2, 4, 0, 7, 1, 6, 5, 3]
@toy
toy / log_red.rb
Last active October 24, 2015 14:10
config/initializers/log_red.rb
def (Rails.logger).red(*args)
s = "\e[31m"
if match = caller.first.match(/^(.*):(\d+)(?:$|:in `)/)
path = match[1]
line_number = match[2].to_i
code = begin
File.open(path) do |f|
(line_number - 1).times { f.gets }
f.gets[/red\s*(.*)\s*/, 1]
end
@toy
toy / which_benchmark.rb
Last active August 29, 2015 14:07
`sh -c 'command -v name'` vs `which name` vs pure ruby
require 'benchmark'
def sh_command_v(name)
path = `sh -c 'command -v #{name}'`.strip
path unless path.empty?
end
def sh_which(name)
path = `which #{name}`.strip
path unless path.empty?
@toy
toy / range_intersection.rb
Created October 8, 2012 20:49
Ruby range intersection
# started with http://www.postal-code.com/binarycode/2009/06/06/better-range-intersection-in-ruby/
# added handling of exclude_end
# fixed slowness with ruby 1.8 as even `0...10000000 & 5000000...15000000` took few seconds to run
class Range
def intersection(other)
raise ArgumentError, 'value must be a Range' unless other.kind_of?(Range)
intersection_begin = case
when self.include?(other.begin)